Entity::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.3329

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 12
cp 0.6667
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 4
crap 7.3329
1
<?php
2
3
namespace Kaliop\eZObjectWrapperBundle\Core;
4
5
use eZ\Publish\API\Repository\Values\Content\Location;
6
use eZ\Publish\API\Repository\Values\Content\Content;
7
use eZ\Publish\API\Repository\Repository as eZRepository;
8
use Kaliop\eZObjectWrapperBundle\Core\Traits\LoggingEntity;
9
use Kaliop\eZObjectWrapperBundle\Core\Traits\EntityManagerAwareEntity;
10
11
class Entity implements EntityInterface
12
{
13
    use LoggingEntity;
14
    use EntityManagerAwareEntity;
15
16
    /**
17
     * @var \eZ\Publish\API\Repository\Repository $repository
18
     */
19
    protected $repository;
20
    /**
21
     * @var \eZ\Publish\API\Repository\Values\Content\Location $location
22
     */
23
    protected $location;
24
    /**
25
     * @var \eZ\Publish\API\Repository\Values\Content\Content $content
26
     */
27
    protected $content;
28
29
    protected $settings;
30
31
    /**
32
     * @param eZRepository $repository
33
     * @param Content $content
34
     * @param Location $location
35
     * @param array $settings used to pass to the Entity instance anything that would be done via DIC if it was a service
36
     * @throws \InvalidArgumentException
37
     */
38 10
    public function __construct(eZRepository $repository, Content $content=null, Location $location=null, array $settings = array())
39
    {
40 10
        if ($content == null && $location == null) {
41
            throw new \InvalidArgumentException('Trying to create Entity with no content or location');
42
        }
43 10
        if ($content != null && $location != null) {
44
            if ($location->contentId != $content->id)
45
                throw new \InvalidArgumentException('Trying to create Entity with mismatching content and location');
46
        }
47 10
        $this->content = $content;
48 10
        $this->location = $location;
49 10
        $this->repository = $repository;
50 10
        $this->settings = $this->validateSettings($settings);
51 10
    }
52
53
    /**
54
     * Called from the constructor, with the settings received from the caller.
55
     * Subclasses can implement checking here, or merge the received settings with other data, using f.e. the Symfony
56
     * OptionsResolver component (see http://symfony.com/doc/current/components/options_resolver.html).
57
     *
58
     * @param array $settings
59
     * @return array
60
     */
61 10
    protected function validateSettings(array $settings)
62
    {
63 10
        return $settings;
64
    }
65
66
    /**
67
     * Return the content of the current location
68
     * @return \eZ\Publish\API\Repository\Values\Content\Content
69
     */
70 9
    public function content()
71
    {
72 9
        if ($this->content == null) {
73 3
            $this->content = $this->repository->getContentService()->loadContent($this->location->contentId);
74 3
        }
75 9
        return $this->content;
76
    }
77
78
    /**
79
     * NB: if entity has been created from a Content, returns its MAIN location. Otherwise, the Location set at creation
80
     * @return \eZ\Publish\API\Repository\Values\Content\Location
81
     */
82 5
    public function location()
83
    {
84 5
        if ($this->location == null) {
85 5
            $this->location = $this->repository->getLocationService()->loadLocation($this->content->contentInfo->mainLocationId);
86 5
        }
87 5
        return $this->location;
88
    }
89
}
90