GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c7f4e3...a41792 )
by Mario
18:09
created

DomainObjectMapper::mapAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Mapper;
6
7
use DateTimeImmutable;
8
use DateTimeInterface;
9
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
10
use eZ\Publish\API\Repository\Repository;
11
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
12
use eZ\Publish\API\Repository\Values\Content\Content as APIContent;
13
use Netgen\InformationCollection\API\Value\Attribute;
14
use Netgen\InformationCollection\API\Value\AttributeValue;
15
use Netgen\InformationCollection\API\Value\Collection;
16
use Netgen\InformationCollection\API\Value\Content;
17
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection;
18
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute;
19
20
final class DomainObjectMapper
21
{
22
    /**
23
     * @var \eZ\Publish\API\Repository\Repository
24
     */
25
    protected $repository;
26
27
    /**
28
     * @var \eZ\Publish\API\Repository\ContentService
29
     */
30
    protected $contentService;
31
32
    /**
33
     * @var \eZ\Publish\API\Repository\ContentTypeService
34
     */
35
    protected $contentTypeService;
36
37
    /**
38
     * @var \eZ\Publish\API\Repository\UserService
39
     */
40
    protected $userService;
41
42
    public function __construct(Repository $repository)
43
    {
44
        $this->repository = $repository;
45
        $this->contentService = $repository->getContentService();
46
        $this->contentTypeService = $repository->getContentTypeService();
47
    }
48
49
    public function mapContent(array $data, EzInfoCollection $first, EzInfoCollection $last, int $childCount): Content
50
    {
51
        $content = $this->contentService->loadContent((int) $data['content_id']);
52
        $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
53
        $hasLocation = empty($object['main_node_id']) ? false : true;
0 ignored issues
show
Bug introduced by
The variable $object seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
54
55
        return new Content(
56
            $content,
57
            $contentType,
58
            $this->mapCollection($first, []),
59
            $this->mapCollection($last, []),
60
            $childCount,
61
            $hasLocation
62
        );
63
    }
64
65
    public function mapCollection(EzInfoCollection $collection, array $attributes): Collection
66
    {
67
        $content = $this->contentService->loadContent($collection->getContentObjectId());
68
        $contentType = $this->contentTypeService
69
            ->loadContentType(
70
                $content->contentInfo->contentTypeId
71
            );
72
73
        $attributeValues = [];
74
        foreach ($attributes as $attr) {
75
            if (empty($contentType->fieldDefinitionsById[$attr->getContentClassAttributeId()])) {
0 ignored issues
show
Bug introduced by
The property fieldDefinitionsById does not seem to exist. Did you mean fieldDefinitions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
76
                continue;
77
            }
78
79
            $attributeValues[] = $this->mapAttribute($attr, $content, $contentType->fieldDefinitionsById[$attr->getContentClassAttributeId()]);
0 ignored issues
show
Bug introduced by
The property fieldDefinitionsById does not seem to exist. Did you mean fieldDefinitions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
        }
81
82
        $user = $this->getUser($collection->getCreatorId());
83
84
        return new Collection(
85
            $collection->getId(),
86
            $content,
87
            $user,
88
            $this->getDateTime($collection->getCreated()),
89
            $this->getDateTime($collection->getModified()),
90
            $attributeValues
91
        );
92
    }
93
94
    public function mapAttribute(EzInfoCollectionAttribute $attribute, APIContent $content, FieldDefinition $fieldDefinition): Attribute
95
    {
96
        $classField = null;
97
        foreach ($content->getFields() as $field) {
98
            if ($field->id == $attribute->getContentObjectAttributeId()) {
99
                $classField = $field;
100
                break;
101
            }
102
        }
103
104
        $value = new AttributeValue($attribute->getDataInt(), $attribute->getDataFloat(), $attribute->getDataText());
105
        dump($classField);
106
        return new Attribute(
107
            $attribute->getId(), $content, $classField, $fieldDefinition, $value
0 ignored issues
show
Bug introduced by
It seems like $classField defined by null on line 96 can be null; however, Netgen\InformationCollec...ttribute::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
108
        );
109
    }
110
111
    protected function getUser($userId)
112
    {
113
        try {
114
            return $this->repository
115
                ->getUserService()
116
                ->loadUser($userId);
117
118
        } catch (NotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
119
120
        }
121
    }
122
123
    protected function getDateTime(int $timestamp): DateTimeInterface
124
    {
125
        $date = new DateTimeImmutable();
126
        $date->setTimestamp($timestamp);
127
128
        return $date;
129
    }
130
}
131