UniSharp /
pricing
| 1 | <?php |
||
| 2 | |||
| 3 | namespace UniSharp\Pricing; |
||
| 4 | |||
| 5 | use Illuminate\Contracts\Pipeline\Pipeline; |
||
| 6 | use Illuminate\Contracts\Container\Container; |
||
| 7 | use UniSharp\Cart\CartItemCollection as Collection; |
||
|
0 ignored issues
–
show
|
|||
| 8 | use UniSharp\Pricing\Exceptions\InvalidModuleException; |
||
| 9 | |||
| 10 | class Pricing |
||
| 11 | { |
||
| 12 | const MODULE_LEVEL = 2; |
||
| 13 | |||
| 14 | protected $items; |
||
| 15 | protected $modules = []; |
||
| 16 | protected $container; |
||
| 17 | protected $pipeline; |
||
| 18 | protected $fees = []; |
||
| 19 | protected $deductions = []; |
||
| 20 | protected $infos = []; |
||
| 21 | protected $logs = []; |
||
| 22 | |||
| 23 | public function __construct(Container $container, Pipeline $pipeline, array $modules) |
||
| 24 | { |
||
| 25 | $this->container = $container; |
||
| 26 | $this->pipeline = $pipeline; |
||
| 27 | $this->modules = $modules; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function setItems(Collection $items) |
||
| 31 | { |
||
| 32 | $this->items = $items; |
||
| 33 | |||
| 34 | return $this; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function getItems() |
||
| 38 | { |
||
| 39 | return $this->items; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function getOriginalTotal() |
||
| 43 | { |
||
| 44 | return $this->items->sum(function ($item) { |
||
| 45 | return $item->price * $item->quantity; |
||
| 46 | }); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getTotal() |
||
| 50 | { |
||
| 51 | $total = $this->getOriginalTotal(); |
||
| 52 | $fees = array_sum($this->getFees()); |
||
| 53 | $deductions = array_sum($this->getDeductions()); |
||
| 54 | |||
| 55 | $result = $total - $deductions + $fees; |
||
| 56 | |||
| 57 | return $result > 0 ? $result : 0; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function apply($module, $params = null) |
||
| 61 | { |
||
| 62 | $this->checkModule($module); |
||
| 63 | |||
| 64 | if ($params) { |
||
| 65 | $this->applyModuleInfo($module, $params); |
||
| 66 | } |
||
| 67 | |||
| 68 | return $this->pipeline |
||
| 69 | ->send($this) |
||
| 70 | ->through($module) |
||
| 71 | ->then(function ($pricing) { |
||
| 72 | return $pricing; |
||
| 73 | }); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function execute() |
||
| 77 | { |
||
| 78 | foreach ($this->getAppliedModules() as $module) { |
||
| 79 | $this->container->call($module . '@finish', [ |
||
| 80 | 'pricing' => $this |
||
| 81 | ]); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function with(array $params) |
||
| 88 | { |
||
| 89 | foreach ($params as $key => $value) { |
||
| 90 | $this->checkModule($key); |
||
| 91 | $this->applyModuleInfo($key, $value); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function applyModuleInfo($module, $params) |
||
| 98 | { |
||
| 99 | $this->infos[$module] = $params; |
||
| 100 | |||
| 101 | return $this; |
||
| 102 | } |
||
| 103 | |||
| 104 | protected function checkModule($module) |
||
| 105 | { |
||
| 106 | if (! in_array($module, $this->modules)) { |
||
| 107 | throw new InvalidModuleException($module . ' not found in module list.'); |
||
| 108 | } |
||
| 109 | |||
| 110 | $reflection = new \ReflectionClass($module); |
||
| 111 | |||
| 112 | if (! $reflection->implementsInterface(ModuleContract::class)) { |
||
| 113 | throw new InvalidModuleException($module . ' must implement ' . ModuleContract::class); |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach ($this->getAppliedModules() as $appliedModule) { |
||
| 117 | if (array_search($module, $this->modules) < array_search($appliedModule, $this->modules)) { |
||
| 118 | throw new InvalidModuleException($module . ' can not apply after ' . $appliedModule); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | public function setModules(array $modules) |
||
| 124 | { |
||
| 125 | $this->modules = $modules; |
||
| 126 | |||
| 127 | return $this; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getModules() |
||
| 131 | { |
||
| 132 | return $this->modules; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function addFee($value, $moduleName = null) |
||
| 136 | { |
||
| 137 | $moduleName = $moduleName ?: $this->getCallerClass(); |
||
| 138 | $this->fees[$moduleName] = $value; |
||
| 139 | |||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function addDeduction($value, $moduleName = null) |
||
| 144 | { |
||
| 145 | $moduleName = $moduleName ?: $this->getCallerClass(); |
||
| 146 | $this->deductions[$moduleName] = $value; |
||
| 147 | |||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function writeModuleLog($value, $moduleName = null) |
||
| 152 | { |
||
| 153 | $moduleName = $moduleName ?: $this->getCallerClass(); |
||
| 154 | $this->logs[$moduleName] = $value; |
||
| 155 | |||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getDeduction($module) |
||
| 160 | { |
||
| 161 | return $this->deductions[$module] ?? null; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function getDeductions() |
||
| 165 | { |
||
| 166 | return $this->deductions; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getFee($module) |
||
| 170 | { |
||
| 171 | return $this->fees[$module] ?? null; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function getFees() |
||
| 175 | { |
||
| 176 | return $this->fees; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getModuleInfo($module = null) |
||
| 180 | { |
||
| 181 | return $this->infos[$module ?? $this->getCallerClass()] ?? null; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function getModuleLog($module = null) |
||
| 185 | { |
||
| 186 | return $this->logs[$module ?? $this->getCallerClass()] ?? null; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function getAppliedModules() |
||
| 190 | { |
||
| 191 | return array_unique(array_merge( |
||
| 192 | array_keys($this->fees), |
||
| 193 | array_keys($this->deductions) |
||
| 194 | )); |
||
| 195 | } |
||
| 196 | |||
| 197 | protected function getCallerClass() |
||
| 198 | { |
||
| 199 | return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[static::MODULE_LEVEL]['class']; |
||
| 200 | } |
||
| 201 | } |
||
| 202 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths