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 ( b5dfb6...a568a9 )
by Mario
40:05
created

ExporterService::getCreatedDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Service;
6
7
use Netgen\Bundle\InformationCollectionBundle\API\Service\Exporter;
8
use Netgen\Bundle\InformationCollectionBundle\API\Value\Export\Export;
9
use Netgen\Bundle\InformationCollectionBundle\API\Value\Export\ExportCriteria;
10
use Netgen\Bundle\InformationCollectionBundle\Core\Persistence\ContentTypeUtils;
11
use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollection;
12
use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollectionAttribute;
13
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository;
14
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository;
15
use Symfony\Component\Translation\TranslatorInterface;
16
17
class ExporterService implements Exporter
18
{
19
    /**
20
     * @var \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository
21
     */
22
    protected $ezInfoCollectionRepository;
23
24
    /**
25
     * @var \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository
26
     */
27
    protected $ezInfoCollectionAttributeRepository;
28
29
    /**
30
     * @var TranslatorInterface
31
     */
32
    protected $translator;
33
34
    /**
35
     * @var ContentTypeUtils
36
     */
37
    protected $contentTypeUtils;
38
39
    /**
40
     * ExporterService constructor.
41
     *
42
     * @param \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository $ezInfoCollectionRepository
43
     * @param \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository
44
     * @param \Symfony\Component\Translation\TranslatorInterface $translator
45
     * @param \Netgen\Bundle\InformationCollectionBundle\Core\Persistence\ContentTypeUtils $contentTypeUtils
46
     */
47
    public function __construct(
48
        EzInfoCollectionRepository $ezInfoCollectionRepository,
49
        EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository,
50
        TranslatorInterface $translator,
51
        ContentTypeUtils $contentTypeUtils
52
    ) {
53
        $this->ezInfoCollectionRepository = $ezInfoCollectionRepository;
54
        $this->ezInfoCollectionAttributeRepository = $ezInfoCollectionAttributeRepository;
55
        $this->translator = $translator;
56
        $this->contentTypeUtils = $contentTypeUtils;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function export(ExportCriteria $criteria)
63
    {
64
        $fields = $this->contentTypeUtils->getInfoCollectorFields($criteria->content->id);
65
        $fields['created'] = $this->translator->trans('netgen_information_collection_admin_export_created', [], 'netgen_information_collection_admin');
66
67
        $collections = $this->ezInfoCollectionRepository->findBy(['contentObjectId' => $criteria->content->id]);
68
69
        $rows = [];
70
71
        /** @var EzInfoCollection $collection */
72
        foreach ($collections as $collection) {
73
            $row = [];
74
            $attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]);
75
76
            foreach ($fields as $fieldId => $fieldName) {
77
                if ($fieldId === 'created') {
78
                    $row[] = $this->getCreatedDate($collection);
79
                    continue;
80
                }
81
82
                $row[] = $this->getAttributeValue($fieldId, $attributes);
83
            }
84
85
            $rows[] = $row;
86
        }
87
88
        $header = array_values($fields);
89
90
        return new Export(
91
            [
92
                'header' => $header,
93
                'contents' => $rows,
94
            ]
95
        );
96
    }
97
98
    /**
99
     * Get create date from EzInfoCollection as string.
100
     *
101
     * @param \Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollection $ezInfoCollection
102
     *
103
     * @return string
104
     */
105
    protected function getCreatedDate(EzInfoCollection $ezInfoCollection)
106
    {
107
        $date = new \DateTimeImmutable();
108
        $date->setTimestamp($ezInfoCollection->getCreated());
109
110
        return $date->format('Y-m-d');
111
    }
112
113
    /**
114
     * Get attribute value string.
115
     *
116
     * @param int $fieldId
117
     * @param array $attributes
118
     *
119
     * @return string
120
     */
121
    protected function getAttributeValue($fieldId, $attributes)
122
    {
123
        /** @var EzInfoCollectionAttribute $attribute */
124
        foreach ($attributes as $attribute) {
125
            if ($fieldId === $attribute->getContentClassAttributeId()) {
126
                $value = $attribute->getValue();
127
                $value = str_replace('"', '""', $value);
128
                $value = str_replace(';', ', ', $value);
129
                $value = strip_tags($value);
130
131
                return preg_replace(['/\r|\n/'], [' '], $value);
132
            }
133
        }
134
135
        return '';
136
    }
137
}
138