public function calculate($price, PriceCalculator $pricingStrategy) { $grandPrice = $price; $grandPrice += $pricingStrategy->negativeDiscount($price); $grandPrice += $pricingStrategy->calculateTaxes($price); $grandPrice = $pricingStrategy->convertCurrency($price); return $grandPrice; }
public function calculate($price, PriceCalculator $pricingStrategy) { $this->grandPrice = $price; $this->grandPrice += $pricingStrategy->addTaxes($this->grandPrice); $this->grandPrice -= $pricingStrategy->applyDiscounts($this->grandPrice); $this->grandPrice = $pricingStrategy->convertCurrencies($this->grandPrice); return $this->grandPrice; }
public function testGetLowestPrice() { $priceCalculator = new PriceCalculator($this->priceLists); $results = [$priceCalculator->getLowestPrice('21171334'), $priceCalculator->getLowestPrice('467352080'), $priceCalculator->getLowestPrice('3372982'), $priceCalculator->getLowestPrice('497205731'), $priceCalculator->getLowestPrice('1'), $priceCalculator->getLowestPrice('humdum')]; $this->assertEquals(0.92, $results[0]['price']); $this->assertEquals(0.44, $results[1]['price']); $this->assertEquals('4673', $results[1]['prefix']); $this->assertEquals(4, $results[2]['price']); $this->assertEquals('Tele2', $results[2]['operator']); $this->assertEquals(1.25, $results[3]['price']); $this->assertEquals(0.95, $results[4]['price']); $this->assertEquals($priceCalculator->defaultResponse, $results[5]); $this->assertNull($results[5]['price']); }
<?php require 'vendor/autoload.php'; require './price-calculator.php'; $app = new \Slim\Slim(); $app->add(new \SlimJson\Middleware(array('json.status' => true))); $operators = [['name' => 'Operatör A', 'entries' => [['prefix' => '1', 'price' => 0.9], ['prefix' => '268', 'price' => 5.1], ['prefix' => '46', 'price' => 0.17], ['prefix' => '4620', 'price' => 0.0], ['prefix' => '468', 'price' => 0.15], ['prefix' => '4631', 'price' => 0.15], ['prefix' => '4673', 'price' => 0.9], ['prefix' => '46732', 'price' => 1.1]]], ['name' => 'Operatör B', 'entries' => [['prefix' => '1', 'price' => 0.92], ['prefix' => '44', 'price' => 0.5], ['prefix' => '46', 'price' => 0.2], ['prefix' => '467', 'price' => 1.0], ['prefix' => '48', 'price' => 1.2]]]]; $app->get('/price/:phoneNumber', function ($phoneNumber) use($app, $operators) { $calculator = new PriceCalculator($operators); $cheapest = $calculator->getLowestPrice($phoneNumber); if (isset($cheapest['price'])) { $app->render(200, $cheapest); } else { $app->render(404, ['error' => 'No results found']); } }); $app->run();