Skip to content

Commit

Permalink
Inspection API calls and functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeymitr committed Mar 15, 2017
1 parent 2b88d2d commit 6544548
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 9 deletions.
57 changes: 57 additions & 0 deletions lib/Endpoints/VehicleInspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Automile\Sdk\Exceptions\AutomileException;
use Automile\Sdk\Config;
use Automile\Sdk\Models\Vehicle\Inspection;
use Automile\Sdk\Models\Vehicle\InspectionExport;
use Automile\Sdk\Models\Vehicle\InspectionRowset;

/**
Expand All @@ -26,6 +27,7 @@ public function getByInspectionId($id)
}

/**
* Get all vehicle inspections that the user has access to
* @param int $vehicleInspectionId
* @param int $vehicleId
* @param \DateTime $fromDate
Expand Down Expand Up @@ -61,4 +63,59 @@ public function getByInspectionIdVehicleAndDate($vehicleInspectionId = null, $ve
throw new AutomileException($errorMessage ?: "Error code: {$response->getStatusCode()}");
}

/**
* Creates a new vehicle inspection
* @param Inspection $inspection
* @return Inspection
*/
public function createInspection(Inspection $inspection)
{
return $this->_create($this->_vehicleInspectionUri, $inspection);
}

/**
* Updates the given vehicle inspection with new model
* @param Inspection $inspection
* @return Inspection
* @throws AutomileException
*/
public function editInspection(Inspection $inspection)
{
if (!$inspection->getVehicleInspectionId())
{
throw new AutomileException('Vehicle Inspection ID is missing');
}

return $this->_edit($this->_vehicleInspectionUri, $inspection->getVehicleInspectionId(), $inspection);
}

/**
* Export a vehicle inspection in pdf format via email
* @param int $vehicleInspectionId
* @param InspectionExport $model
* @return bool
* @throws AutomileException
*/
public function exportVehicleInspection($vehicleInspectionId, InspectionExport $model)
{
$request = Config::getNewRequest();
$response = Config::getNewResponse();
$client = Config::getNewHttpClient();

$this->_authorizeRequest($request);

$request->setMethod(Config::METHOD_POST)
->setUri($this->_vehicleInspectionUri . '/export/' . $vehicleInspectionId)
->setBody($model->toJson())
->setContentType('application/json');

$isSuccessful = $client->send($request, $response);

if ($isSuccessful) {
return true;
}

$errorMessage = $response->getErrorMessage();
throw new AutomileException($errorMessage ?: "Error code: {$response->getStatusCode()}");
}
}
4 changes: 2 additions & 2 deletions lib/Models/ExpenseReportRowContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* @method int getExpenseReportRowId()
* @method int getContentType()
* @method string getContentFileName()
* @method string getData()
* @method string getDataFile()
* @method string getData() base64-encoded raw data upon uploading
* @method string getDataFile() path to the attachment file upon uploading
*
* @method ExpenseReportRowContent setExpenseReportRowContentId(int $expenseReportRowContentId)
* @method ExpenseReportRowContent setExpenseReportRowId(int $expenseReportRowId)
Expand Down
45 changes: 42 additions & 3 deletions lib/Models/Vehicle/Defect.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
* @method Defect setCreatedByContactId(int $contactId)
* @method Defect setNotes(string $notes)
* @method Defect setDefectStatusType(int $statusType)
* @method Defect setVehicleDefectStatus(array|object $statuses)
* @method Defect setVehicleDefectAttachments(array|object $attachments)
* @method Defect setVehicleDefectComments(array|object $comments)
*/
class Defect extends ModelAbstract
{
Expand All @@ -44,6 +41,48 @@ class Defect extends ModelAbstract
'VehicleDefectComments'
];

/**
* @param array|object $status
* @return Defect
*/
public function setVehicleDefectStatus($status)
{
if (!is_object($status) || !$status instanceof DefectStatusRowset) {
$status = new DefectStatusRowset($status);
}

$this->_properties['VehicleDefectStatus'] = $status;
return $this;
}

/**
* @param array|object $comments
* @return Defect
*/
public function setVehicleDefectComments($comments)
{
if (!is_object($comments) || !$comments instanceof DefectCommentRowset) {
$comments = new DefectCommentRowset($comments);
}

$this->_properties['VehicleDefectComments'] = $comments;
return $this;
}

/**
* @param array|object $attachments
* @return Defect
*/
public function setVehicleDefectAttachments($attachments)
{
if (!is_object($attachments) || !$attachments instanceof DefectAttachmentRowset) {
$attachments = new DefectAttachmentRowset($attachments);
}

$this->_properties['VehicleDefectAttachments'] = $attachments;
return $this;
}

/**
* @param string|\DateTime $dateTime a DateTime object or date in string representation
* @return Defect
Expand Down
40 changes: 39 additions & 1 deletion lib/Models/Vehicle/DefectAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Automile\Sdk\Models\Vehicle;

use Automile\Sdk\Models\ModelAbstract;
use Automile\Sdk\Models\ModelException;
use Automile\Sdk\Types\AttachmentType;

/**
* Vehicle DefectAttachment Model
Expand All @@ -12,12 +14,16 @@
* @method string getAttachmentType()
* @method string getAttachmentLocation()
* @method string getAttachmentDateUtc()
* @method string getData() base64-encoded raw data upon uploading
* @method string getDataFile() path to the attachment file upon uploading
*
* @method DefectAttachment setVehicleDefectAttachmentId(int $attachmentId)
* @method DefectAttachment setVehicleDefectId(int $defectId)
* @method DefectAttachment setAttachmentType(string $type)
* @method DefectAttachment setAttachmentLocation(string $location)
* @method DefectAttachment setAttachmentDateUtc(string $date)
* @method DefectAttachment setData(string $data) base64-encoded raw data upon uploading
* @method DefectAttachment setDataFile(string $dataFile) path to the attachment file upon uploading
*/
class DefectAttachment extends ModelAbstract
{
Expand All @@ -27,7 +33,39 @@ class DefectAttachment extends ModelAbstract
'VehicleDefectId',
'AttachmentType',
'AttachmentLocation',
'AttachmentDateUtc'
'AttachmentDateUtc',
'Data',
'DataFile'
];

/**
* @return array
* @throws ModelException
*/
public function toArray()
{
$data = parent::toArray();

if (!empty($data['DataFile'])) {
if (!empty($data['Data'])) {
throw new ModelException('Content data already exists');
}

$content = @file_get_contents($data['DataFile']);
if (!$content) {
throw new ModelException("Data File is empty or inaccessible: '{$data['DataFile']}");
}

$data['Data'] = base64_encode($content);

if (!isset($data['AttachmentType'])) {
$data['AttachmentType'] = AttachmentType::getByFilename($data['DataFile']);
}

unset($data['DataFile']);
}

return $data;
}

}
18 changes: 18 additions & 0 deletions lib/Models/Vehicle/InspectionExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Automile\Sdk\Models\Vehicle;

use Automile\Sdk\Models\ModelAbstract;

/**
* InspectionExport Vehicle Model
*/
class InspectionExport extends ModelAbstract
{

protected $_allowedProperties = [
'ToEmail',
'ISO639LanguageCode'
];

}
56 changes: 56 additions & 0 deletions lib/Types/AttachmentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Automile\Sdk\Types;

/**
* Attachment Type
*/
class AttachmentType implements Type
{

const IMAGE = 0;
const PDF = 1;
const DOCX = 2;
const EXCEL = 3;

/**
* @param mixed $value
* @return bool
*/
public static function isValid($value)
{
return in_array($value, rante(0, 3));
}

/**
* determine attachment type by file extension
* @param string $filename
* @return int
*/
public static function getByFilename($filename)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);

switch ($ext)
{
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
case 'tiff':
case 'bmp':
return self::IMAGE;

case 'pdf':
return self::PDF;

case 'xls':
case 'xlsx':
return self::EXCEL;

case 'docx':
return self::DOCX;
}
}

}
16 changes: 15 additions & 1 deletion lib/Types/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* ContentType Enum
*/
class ContentType
class ContentType implements Type
{

const IMAGE = 0;
Expand All @@ -14,6 +14,11 @@ class ContentType
const EXCEL = 3;
const WORD = 4;

/**
* determine content type by file extension
* @param string $filename
* @return int
*/
public static function getByFilename($filename)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
Expand Down Expand Up @@ -46,4 +51,13 @@ public static function getByFilename($filename)
}
}

/**
* @param mixed $value
* @return bool
*/
public static function isValid($value)
{
return in_array($value, rante(0, 4));
}

}
23 changes: 23 additions & 0 deletions lib/Types/VehicleDefectStatusType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Automile\Sdk\Types;

/**
* VehicleDefectStatus Type
*/
class VehicleDefectStatusType implements Type
{

const NOT_RESOLVED = 0;
const RESOLVED = 1;

/**
* @param mixed $value
* @return bool
*/
public static function isValid($value)
{
return in_array($value, range(0, 1));
}

}
Loading

0 comments on commit 6544548

Please sign in to comment.