ListCollectionFactory::create()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 4
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the InMemoryList package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 */
10
11
namespace InMemoryList\Infrastructure\Domain\Model;
12
13
use InMemoryList\Domain\Model\Contracts\ListFactoryInterface as Factory;
14
use InMemoryList\Domain\Model\ListCollection;
15
use InMemoryList\Domain\Model\ListCollectionUuid;
16
use InMemoryList\Domain\Model\ListElement;
17
use InMemoryList\Domain\Model\ListElementUuid;
18
use InMemoryList\Infrastructure\Domain\Model\Exceptions\CreateListFromEmptyArrayException;
19
use InMemoryList\Infrastructure\Domain\Model\Exceptions\NotValidKeyElementInListException;
20
21
class ListCollectionFactory implements Factory
22
{
23
    /**
24
     * @param array $elements
25
     * @param array $headers
26
     * @param null  $uuid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $uuid is correct as it would always require null to be passed?
Loading history...
27
     * @param null  $elementUuid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $elementUuid is correct as it would always require null to be passed?
Loading history...
28
     *
29
     * @return ListCollection
30
     *
31
     * @throws CreateListFromEmptyArrayException
32
     */
33
    public function create(array $elements, array $headers = [], $uuid = null, $elementUuid = null)
34
    {
35
        if (empty($elements)) {
36
            throw new CreateListFromEmptyArrayException('Try to create a collection from an empty array.');
37
        }
38
39
        $listUuid = new ListCollectionUuid($uuid);
40
        $list = new ListCollection($listUuid);
41
42
        foreach ($elements as $element) {
43
            $newElementUuid = new ListElementUuid(($elementUuid) ? (string) $this->getValueFromKey($element, $elementUuid) : null);
0 ignored issues
show
Bug introduced by
It seems like $elementUuid ? (string)$...t, $elementUuid) : null can also be of type string; however, parameter $uuid of InMemoryList\Domain\Mode...mentUuid::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            $newElementUuid = new ListElementUuid(/** @scrutinizer ignore-type */ ($elementUuid) ? (string) $this->getValueFromKey($element, $elementUuid) : null);
Loading history...
44
            $list->addElement(new ListElement($newElementUuid, $element));
45
        }
46
47
        if ($headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
            $list->setHeaders($headers);
49
        }
50
51
        return $list;
52
    }
53
54
    /**
55
     * @param $element
56
     * @param $key
57
     *
58
     * @return mixed
59
     *
60
     * @throws NotValidKeyElementInListException
61
     */
62
    private function getValueFromKey($element, $key)
63
    {
64
        if ((is_object($element) && !$this->getValueKeyFromObject($element, $key)) || (is_array($element) && !isset($element[$key]))) {
65
            $getterName = 'get'.str_replace(' ', '', ucwords($key));
66
67
            throw new NotValidKeyElementInListException($key.' is not a valid key. If your elements are Entities class, please check if you implement '.$getterName.'() method.');
68
        }
69
70
        return is_object($element) ? $this->getValueKeyFromObject($element, $key) : $element[$key];
71
    }
72
73
    /**
74
     * @param $element
75
     * @param $key
76
     *
77
     * @return bool
78
     */
79
    private function getValueKeyFromObject($element, $key)
80
    {
81
        if (!$element instanceof \stdClass) {
82
            $getterName = 'get'.str_replace(' ', '', ucwords($key));
83
84
            return (method_exists($element, $getterName)) ? $element->$getterName() : false;
85
        }
86
87
        return (isset($element->{$key})) ? $element->{$key} : false;
88
    }
89
}
90