| Conditions | 3 |
| Paths | 1 |
| Total Lines | 18 |
| Code Lines | 8 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 3 |
| CRAP Score | 3 |
| Changes | 0 | ||
| 1 | <?php |
||
| 31 | 4 | public function create(array $data) |
|
| 32 | { |
||
| 33 | 4 | if (!is_callable($this->factoryMethod)) { |
|
| 34 | 1 | throw LogicException::factoryMethodIsNotProvided($this->fields); |
|
| 35 | } |
||
| 36 | |||
| 37 | // use only data indicated in $fields array |
||
| 38 | $data = array_filter($data, function ($key) { return in_array($key, $this->fields); }, ARRAY_FILTER_USE_KEY); |
||
| 39 | |||
| 40 | // flip $fields to have it's value as keys in $defaults array and reset all values to null |
||
| 41 | $defaults = array_map(function () { return null; }, array_flip($this->fields)); |
||
| 42 | $command = call_user_func($this->factoryMethod, array_merge($defaults, $data)); |
||
| 43 | |||
| 44 | if (!is_object($command)) { |
||
| 45 | throw LogicException::commandIsNotAnObject(); |
||
| 46 | } |
||
| 47 | |||
| 48 | return $command; |
||
| 49 | } |
||
| 51 |