TextnodeController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
namespace AdminBundle\Controller;
20
21
use DembeloMain\Document\TextnodeHitch;
22
use DembeloMain\Model\Repository\ImportfileRepositoryInterface;
23
use DembeloMain\Model\Repository\LicenseeRepositoryInterface;
24
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
25
use Doctrine\Common\Collections\Collection;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
use Symfony\Component\HttpFoundation\Response;
28
29
/**
30
 * Class TextnodeController
31
 * @Route(service="app.admin_controller_textnode")
32
 */
33
class TextnodeController
34
{
35
    /**
36
     * @var TextNodeRepositoryInterface
37
     */
38
    private $textnodeRepository;
39
40
    /**
41
     * @var ImportfileRepositoryInterface
42
     */
43
    private $importfileRepository;
44
45
    /**
46
     * @var LicenseeRepositoryInterface
47
     */
48
    private $licenseeRepository;
49
50
    /**
51
     * TextnodeController constructor.
52
     * @param TextnodeRepositoryInterface   $textnodeRepository
53
     * @param ImportfileRepositoryInterface $importfileRepository
54
     * @param LicenseeRepositoryInterface   $licenseeRepository
55
     */
56 1
    public function __construct(TextnodeRepositoryInterface $textnodeRepository, ImportfileRepositoryInterface $importfileRepository, LicenseeRepositoryInterface $licenseeRepository)
57
    {
58 1
        $this->textnodeRepository = $textnodeRepository;
59 1
        $this->importfileRepository = $importfileRepository;
60 1
        $this->licenseeRepository = $licenseeRepository;
61 1
    }
62
63
    /**
64
     * @Route("/textnodes", name="admin_textnodes")
65
     *
66
     * @return Response
67
     *
68
     * @throws \InvalidArgumentException
69
     */
70 1
    public function textnodesAction(): Response
71
    {
72 1
        $textnodes = $this->textnodeRepository->findAll();
73
74 1
        $licenseeIndex = $this->buildLicenseeIndex();
75 1
        $importfileIndex = $this->buildImportfileIndex();
76
77 1
        $output = [];
78 1
        foreach ($textnodes as $textnode) {
79 1
            $obj = new \stdClass();
80 1
            $obj->id = $textnode->getId();
81 1
            $obj->status = $textnode->getStatus() ? 'aktiv' : 'inaktiv';
82 1
            $obj->created = $textnode->getCreated()->format('d.m.Y, H:i:s');
83 1
            $obj->access = $textnode->getAccess() ? 'ja' : 'nein';
84 1
            $obj->licensee = $licenseeIndex[$textnode->getLicenseeId()];
85 1
            $obj->importfile = $importfileIndex[$textnode->getImportfileId()] ?? 'unbekannt';
86 1
            $obj->beginning = substr(htmlentities(strip_tags($textnode->getText())), 0, 200).'...';
87 1
            $obj->financenode = $textnode->isFinanceNode() ? 'ja' : 'nein';
88 1
            $obj->arbitraryId = $textnode->getArbitraryId();
89 1
            $obj->twineId = $textnode->getTwineId();
90 1
            $obj->metadata = $this->formatMetadata($textnode->getMetadata());
91 1
            $obj->parentnodes = $this->buildHitchString($textnode->getParentHitches(), 'parent');
92 1
            $obj->childnodes = $this->buildHitchString($textnode->getChildHitches(), 'child');
93 1
            $output[] = $obj;
94
        }
95
96 1
        return new Response(\json_encode($output));
97
    }
98
99
    /**
100
     * @param TextnodeHitch[]|Collection $hitches
101
     * @param string                     $direction
102
     *
103
     * @return string
104
     */
105 1
    private function buildHitchString(Collection $hitches, string $direction): string
106
    {
107 1
        $string = '';
108 1
        $counter = 0;
109 1
        foreach ($hitches as $hitch) {
110
            ++$counter;
111
112
            if ('parent' === $direction) {
113
                $arbitraryId = $hitch->getSourceTextnode()->getArbitraryId();
114
            } else {
115
                $arbitraryId = $hitch->getTargetTextnode()->getArbitraryId();
116
            }
117
118
            $string .= $counter.') '.$hitch->getDescription().' ['.$arbitraryId.']'."\n";
119
        }
120
121 1
        return $string;
122
    }
123
124
    /**
125
     * @return array
126
     */
127 1
    private function buildLicenseeIndex(): array
128
    {
129 1
        $licensees = $this->licenseeRepository->findAll();
130 1
        $index = [];
131 1
        foreach ($licensees as $licensee) {
132 1
            $index[$licensee->getId()] = $licensee->getName();
133
        }
134
135 1
        return $index;
136
    }
137
138
    /**
139
     * @param array $metadata
140
     *
141
     * @return string
142
     */
143 1
    private function formatMetadata(array $metadata): string
144
    {
145 1
        $string = '';
146 1
        foreach ($metadata as $key => $value) {
147 1
            $string .= $key.': '.$value."\n";
148
        }
149
150 1
        return $string;
151
    }
152
153
    /**
154
     * @return array
155
     */
156 1
    private function buildImportfileIndex(): array
157
    {
158
        /* @var $importfiles \DembeloMain\Document\Importfile[] */
159 1
        $importfiles = $this->importfileRepository->findAll();
160 1
        $index = [];
161 1
        foreach ($importfiles as $importfile) {
162 1
            $index[$importfile->getId()] = $importfile->getName();
163
        }
164
165 1
        return $index;
166
    }
167
}
168