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 ( 832924...ed2c61 )
by Mario
02:03
created

InformationCollectionCollector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 8
dl 0
loc 132
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A collect() 0 11 2
A reset() 0 4 1
A getName() 0 4 1
A getCollections() 0 4 1
A getCollectionCount() 0 4 1
A getContent() 0 4 1
A getContentId() 0 4 1
A getContentType() 0 4 1
A getContentTypeId() 0 4 1
A getAdminSiteaccess() 0 4 1
A getContentTypeGroupId() 0 4 1
A mapCollectedData() 0 44 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\Bundle\InformationCollectionBundle\DataCollector;
6
7
use eZ\Publish\API\Repository\Repository;
8
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
9
use eZ\Publish\Core\Helper\TranslationHelper;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13
use Netgen\InformationCollection\Integration\RepositoryForms\InformationCollectionType;
14
15
class InformationCollectionCollector extends DataCollector
16
{
17
    /**
18
     * @var \eZ\Publish\API\Repository\Repository
19
     */
20
    private $repository;
21
22
    /**
23
     * @var \eZ\Publish\Core\Helper\TranslationHelper
24
     */
25
    private $translationHelper;
26
27
    public function __construct(Repository $repository, TranslationHelper $translationHelper)
28
    {
29
        $this->repository = $repository;
30
        $this->data = [
31
            'count' => 0,
32
            'collections' => [],
33
            'content_type' => null,
34
            'content' => null,
35
        ];
36
37
        $this->translationHelper = $translationHelper;
38
    }
39
40
    public function collect(Request $request, Response $response, \Throwable $exception = null)
41
    {
42
        if ($request->get(InformationCollectionType::FORM_BLOCK_PREFIX) !== null) {
43
            $this->mapCollectedData($request);
44
45
            return;
46
        }
47
48
        $this->data = [];
49
50
    }
51
52
    public function reset(): void
53
    {
54
        $this->data = [];
55
    }
56
57
    public function getName(): string
58
    {
59
        return 'netgen_information_collection_collector';
60
    }
61
62
    public function getCollections(): array
63
    {
64
        return $this->data['collections'] ?? [];
65
    }
66
67
    public function getCollectionCount(): int
68
    {
69
        return $this->data['count'] ?? 0;
70
    }
71
72
    public function getContent(): string
73
    {
74
        return $this->data['content'];
75
    }
76
77
    public function getContentId(): int
78
    {
79
        return $this->data['content_id'];
80
    }
81
82
    public function getContentType(): string
83
    {
84
        return $this->data['content_type'];
85
    }
86
87
    public function getContentTypeId(): int
88
    {
89
        return $this->data['content_type_id'];
90
    }
91
92
    public function getAdminSiteaccess(): string
93
    {
94
        return 'admin';
95
    }
96
97
    public function getContentTypeGroupId(): int
98
    {
99
        return $this->data['content_type_group_id'];
100
    }
101
102
    private function mapCollectedData(Request $request): void
103
    {
104
        $mapped = [];
105
106
        $data = $request->get('information_collection');
107
108
        $contentId = $data['content_id'];
109
        $contentTypeId = intval($data['content_type_id']);
110
        /** @var ContentType $contentType */
111
        $contentType = $this->repository->sudo(
112
            function(Repository $repository) use ($contentTypeId) {
113
                return $repository->getContentTypeService()->loadContentType($contentTypeId);
114
            }
115
        );
116
117
        $content = $this->repository->sudo(
118
            function(Repository $repository) use ($contentId) {
119
                return $repository->getContentService()->loadContent((int)$contentId);
120
            }
121
        );
122
123
        foreach ($data as $identifier => $datum) {
124
            if (is_array($datum) && array_key_exists('value', $datum)) {
125
126
                $fieldDefinition = $contentType->getFieldDefinition($identifier);
127
128
                $mapped['collections'][] = [
129
                    'identifier' => $identifier,
130
                    'value' => $datum['value'],
131
                    'name' => $this->translationHelper->getTranslatedByMethod($fieldDefinition, 'getName'),
0 ignored issues
show
Bug introduced by
It seems like $fieldDefinition defined by $contentType->getFieldDefinition($identifier) on line 126 can be null; however, eZ\Publish\Core\Helper\T...getTranslatedByMethod() 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...
132
                    'type' => $fieldDefinition->fieldTypeIdentifier,
133
                ];
134
            }
135
        }
136
137
        $mapped['content'] = $this->translationHelper->getTranslatedContentName($content);
138
        $mapped['content_id'] = $content->id;
139
        $mapped['content_type'] = $this->translationHelper->getTranslatedByMethod($contentType, 'getName');
140
        $mapped['content_type_id'] = $contentType->id;
141
        $mapped['content_type_group_id'] = $contentType->getContentTypeGroups()[0]->id;
142
        $mapped['count'] = count($mapped['collections']);
143
144
        $this->data = $mapped;
145
    }
146
}
147