Passed
Push — master ( 9fa2bb...0992fe )
by Michael
01:53
created

TextnodeController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 90.74%

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 133
ccs 49
cts 54
cp 0.9074
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A formatMetadata() 0 8 2
A buildLicenseeIndex() 0 9 2
A buildImportfileIndex() 0 10 2
B textnodesAction() 0 27 5
A __construct() 0 5 1
A buildHitchString() 0 17 3
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 Doctrine\ODM\MongoDB\PersistentCollection;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
29
use Symfony\Component\HttpFoundation\Response;
30
31
/**
32
 * Class TextnodeController
33
 * @Route(service="app.admin_controller_textnode")
34
 */
35
class TextnodeController extends Controller
36
{
37
    /**
38
     * @var TextNodeRepositoryInterface
39
     */
40
    private $textnodeRepository;
41
42
    /**
43
     * @var ImportfileRepositoryInterface
44
     */
45
    private $importfileRepository;
46
47
    /**
48
     * @var LicenseeRepositoryInterface
49
     */
50
    private $licenseeRepository;
51
52
    /**
53
     * TextnodeController constructor.
54
     * @param TextnodeRepositoryInterface   $textnodeRepository
55
     * @param ImportfileRepositoryInterface $importfileRepository
56
     * @param LicenseeRepositoryInterface   $licenseeRepository
57
     */
58 1
    public function __construct(TextnodeRepositoryInterface $textnodeRepository, ImportfileRepositoryInterface $importfileRepository, LicenseeRepositoryInterface $licenseeRepository)
59
    {
60 1
        $this->textnodeRepository = $textnodeRepository;
61 1
        $this->importfileRepository = $importfileRepository;
62 1
        $this->licenseeRepository = $licenseeRepository;
63 1
    }
64
65
    /**
66
     * @Route("/textnodes", name="admin_textnodes")
67
     *
68
     * @return Response
69
     *
70
     * @throws \InvalidArgumentException
71
     */
72 1
    public function textnodesAction(): Response
73
    {
74 1
        $textnodes = $this->textnodeRepository->findAll();
75
76 1
        $licenseeIndex = $this->buildLicenseeIndex();
77 1
        $importfileIndex = $this->buildImportfileIndex();
78
79 1
        $output = [];
80 1
        foreach ($textnodes as $textnode) {
81 1
            $obj = new \stdClass();
82 1
            $obj->id = $textnode->getId();
83 1
            $obj->status = $textnode->getStatus() ? 'aktiv' : 'inaktiv';
84 1
            $obj->created = $textnode->getCreated()->format('d.m.Y, H:i:s');
85 1
            $obj->access = $textnode->getAccess() ? 'ja' : 'nein';
86 1
            $obj->licensee = $licenseeIndex[$textnode->getLicenseeId()];
87 1
            $obj->importfile = $importfileIndex[$textnode->getImportfileId()] ?? 'unbekannt';
88 1
            $obj->beginning = substr(htmlentities(strip_tags($textnode->getText())), 0, 200).'...';
89 1
            $obj->financenode = $textnode->isFinanceNode() ? 'ja' : 'nein';
90 1
            $obj->arbitraryId = $textnode->getArbitraryId();
91 1
            $obj->twineId = $textnode->getTwineId();
92 1
            $obj->metadata = $this->formatMetadata($textnode->getMetadata());
93 1
            $obj->parentnodes = $this->buildHitchString($textnode->getParentHitches(), 'parent');
94 1
            $obj->childnodes = $this->buildHitchString($textnode->getChildHitches(), 'child');
95 1
            $output[] = $obj;
96
        }
97
98 1
        return new Response(\json_encode($output));
99
    }
100
101
    /**
102
     * @param TextnodeHitch[]|Collection $hitches
103
     * @param string                     $direction
104
     *
105
     * @return string
106
     */
107 1
    private function buildHitchString(Collection $hitches, string $direction): string
108
    {
109 1
        $string = '';
110 1
        $counter = 0;
111 1
        foreach ($hitches as $hitch) {
112
            ++$counter;
113
114
            if ('parent' === $direction) {
115
                $arbitraryId = $hitch->getSourceTextnode()->getArbitraryId();
116
            } else {
117
                $arbitraryId = $hitch->getTargetTextnode()->getArbitraryId();
118
            }
119
120
            $string .= $counter.') '.$hitch->getDescription().' ['.$arbitraryId.']'."\n";
121
        }
122
123 1
        return $string;
124
    }
125
126
    /**
127
     * @return array
128
     */
129 1
    private function buildLicenseeIndex(): array
130
    {
131 1
        $licensees = $this->licenseeRepository->findAll();
132 1
        $index = [];
133 1
        foreach ($licensees as $licensee) {
134 1
            $index[$licensee->getId()] = $licensee->getName();
135
        }
136
137 1
        return $index;
138
    }
139
140
    /**
141
     * @param array $metadata
142
     *
143
     * @return string
144
     */
145 1
    private function formatMetadata(array $metadata): string
146
    {
147 1
        $string = '';
148 1
        foreach ($metadata as $key => $value) {
149 1
            $string .= $key.': '.$value."\n";
150
        }
151
152 1
        return $string;
153
    }
154
155
    /**
156
     * @return array
157
     */
158 1
    private function buildImportfileIndex(): array
159
    {
160
        /* @var $importfiles \DembeloMain\Document\Importfile[] */
161 1
        $importfiles = $this->importfileRepository->findAll();
162 1
        $index = [];
163 1
        foreach ($importfiles as $importfile) {
164 1
            $index[$importfile->getId()] = $importfile->getName();
165
        }
166
167 1
        return $index;
168
    }
169
}
170