1 <?php
2 3 4
5 namespace Team3\PayU\Order\Autocomplete;
6
7 use Psr\Log\LoggerInterface;
8 use Team3\PayU\Configuration\ConfigurationInterface;
9 use Team3\PayU\Order\Autocomplete\Strategy\AutocompleteStrategyInterface;
10 use Team3\PayU\Order\Model\OrderInterface;
11
12 class OrderAutocomplete implements OrderAutocompleteInterface
13 {
14 15 16
17 private $strategies;
18
19 20 21
22 private $logger;
23
24 25 26
27 public function __construct(LoggerInterface $logger)
28 {
29 $this->strategies = [];
30 $this->logger = $logger;
31 }
32
33 34 35 36
37 public function autocomplete(
38 OrderInterface $order,
39 ConfigurationInterface $configuration
40 ) {
41 foreach ($this->strategies as $strategy) {
42 if ($strategy->supports($order)) {
43 $strategy->autocomplete($order, $configuration);
44 $this->logAutocompletion($order, $strategy);
45 }
46 }
47 }
48
49 50 51 52 53
54 public function addStrategy(
55 AutocompleteStrategyInterface $autocompleteStrategy
56 ) {
57 $this->strategies[] = $autocompleteStrategy;
58
59 return $this;
60 }
61
62 63 64 65
66 private function logAutocompletion(
67 OrderInterface $order,
68 AutocompleteStrategyInterface $strategy
69 ) {
70 $this
71 ->logger
72 ->info(
73 sprintf(
74 'Order with ID %s parameters were autocompleted by %s',
75 $order->getOrderId(),
76 get_class($strategy)
77 ),
78 [
79 'order' => $order,
80 'strategy' => $strategy,
81 ]
82 );
83 }
84 }
85