Skip to content

Commit

Permalink
Merge pull request #94 from MyParcelCOM/develop
Browse files Browse the repository at this point in the history
v1.1.1
  • Loading branch information
Shodske committed Feb 26, 2019
2 parents a4a8f7e + 35fb75a commit 57ddfd6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 87 deletions.
3 changes: 3 additions & 0 deletions bin/mp/update
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

./mp.sh composer install
66 changes: 0 additions & 66 deletions src/MyParcelComApi.php
Expand Up @@ -438,10 +438,6 @@ public function createShipment(ShipmentInterface $shipment)
);
}

if ($shipment->getService() === null || $shipment->getContract() === null) {
$this->determineServiceAndContract($shipment);
}

$validator = new ShipmentValidator($shipment);

if (!$validator->isValid()) {
Expand Down Expand Up @@ -475,68 +471,6 @@ public function updateShipment(ShipmentInterface $shipment)
return $this->patchResource($shipment);
}

/**
* Determine which service and contract to use for given shipment
* and update the shipment.
*
* @param ShipmentInterface $shipment
* @return $this
*/
protected function determineServiceAndContract(ShipmentInterface $shipment)
{
$calculator = new PriceCalculator();

if ($shipment->getService() !== null) {
$services = [$shipment->getService()];
} else {
$services = [];
$collection = $this->getServices($shipment)->limit(30);
for ($offset = 0; $offset < $collection->count(); $offset += 30) {
$services = array_merge($services, $collection->offset($offset)->get());
}
}

$serviceIds = implode(',', array_map(function (ServiceInterface $service) {
return $service->getId();
}, $services));

$filters = [
'weight' => $shipment->getPhysicalProperties()->getWeight(),
'service' => $serviceIds,
];

if ($shipment->getContract() !== null) {
$filters['contract'] = $shipment->getContract()->getId();
};

$serviceRates = $this->getServiceRates($filters);

$rates = [];
foreach ($serviceRates as $serviceRate) {
$price = $calculator->calculate($shipment, $serviceRate);

if ($price === null) {
continue;
}

$rates[] = [
'price' => $price,
'service' => $serviceRate->getService(),
'contract' => $serviceRate->getContract(),
];
}

usort($rates, function ($a, $b) {
return $a['price'] - $b['price'];
});

$cheapest = reset($rates);
$shipment->setService($cheapest['service']);
$shipment->setContract($cheapest['contract']);

return $this;
}

/**
* Set the URI of the MyParcel.com API.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/ShipmentValidator.php
Expand Up @@ -40,7 +40,7 @@ public function isValid()
*/
protected function checkRequired()
{
$required = ['weight', 'service', 'contract', 'recipient_address', 'sender_address', 'shop'];
$required = ['weight', 'recipient_address', 'sender_address', 'shop'];

array_walk($required, function ($required) {
$getter = 'get' . StringUtils::snakeToPascalCase($required);
Expand Down
20 changes: 4 additions & 16 deletions tests/Feature/MyParcelComApiTest.php
Expand Up @@ -88,14 +88,8 @@ public function testCreateMinimumViableShipment()

$shipment = $this->api->createShipment($shipment);

$this->assertNotNull(
$shipment->getService(),
'When no service has been selected, the preferred service for given shipment should be used'
);
$this->assertNotNull(
$shipment->getContract(),
'When no contract has been selected, the preferred contract for given shipment should be used'
);
$this->assertNull($shipment->getService());
$this->assertNull($shipment->getContract());
$this->assertNotNull(
$shipment->getId(),
'Once the shipment has been created, it should have an id'
Expand Down Expand Up @@ -137,14 +131,8 @@ public function testSaveShipment()

$shipment = $this->api->saveShipment($shipment);

$this->assertNotNull(
$shipment->getService(),
'When no service has been selected, the preferred service for given shipment should be used'
);
$this->assertNotNull(
$shipment->getContract(),
'When no contract has been selected, the preferred contract for given shipment should be used'
);
$this->assertNull($shipment->getService());
$this->assertNull($shipment->getContract());
$this->assertNotNull(
$shipment->getId(),
'Once the shipment has been created, it should have an id'
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/ShipmentValidatorTest.php
Expand Up @@ -114,23 +114,23 @@ public function testMissingShop()
}

/** @test */
public function testMissingService()
public function testShipmentWithoutServiceShouldBeValid()
{
$shipment = $this->createShipmentWithoutProperty('service');

$validator = new ShipmentValidator($shipment);

$this->assertFalse($validator->isValid());
$this->assertTrue($validator->isValid());
}

/** @test */
public function testMissingContract()
public function testShipmentWithoutContractShouldBeValid()
{
$shipment = $this->createShipmentWithoutProperty('contract');

$validator = new ShipmentValidator($shipment);

$this->assertFalse($validator->isValid());
$this->assertTrue($validator->isValid());
}

/** @test */
Expand Down

0 comments on commit 57ddfd6

Please sign in to comment.