Completed
Push — master ( 984bef...668191 )
by Michael
04:42
created

TextnodeController::textnodesAction()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 20
cts 20
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 17
nop 0
crap 6
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
20
namespace AdminBundle\Controller;
21
22
use DembeloMain\Model\Repository\ImportfileRepositoryInterface;
23
use DembeloMain\Model\Repository\LicenseeRepositoryInterface;
24
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
25
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
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 extends Controller
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(
57
        TextnodeRepositoryInterface $textnodeRepository,
58
        ImportfileRepositoryInterface $importfileRepository,
59
        LicenseeRepositoryInterface $licenseeRepository
60
    ) {
61 1
        $this->textnodeRepository = $textnodeRepository;
62 1
        $this->importfileRepository = $importfileRepository;
63 1
        $this->licenseeRepository = $licenseeRepository;
64 1
    }
65
66
    /**
67
     * @Route("/textnodes", name="admin_textnodes")
68
     *
69
     * @return Response
70
     * @throws \InvalidArgumentException
71
     */
72 1
    public function textnodesAction(): Response
73
    {
74
        /* @var $textnodes \DembeloMain\Document\Textnode[] */
75 1
        $textnodes = $this->textnodeRepository->findAll();
76
77 1
        $licenseeIndex = $this->buildLicenseeIndex();
78 1
        $importfileIndex = $this->buildImportfileIndex();
79
80 1
        $output = [];
81
        /* @var $textnode \DembeloMain\Document\Textnode */
82 1
        foreach ($textnodes as $textnode) {
83 1
            $obj = new \stdClass();
84 1
            $obj->id = $textnode->getId();
85 1
            $obj->arbitraryId = $textnode->getArbitraryId();
86 1
            $obj->created = $textnode->getCreated()->format('d.m.Y, H:i:s');
87 1
            $obj->status = $textnode->getStatus() ? 'aktiv' : 'inaktiv';
88 1
            $obj->access = $textnode->getAccess() ? 'ja' : 'nein';
89 1
            $obj->licensee = $licenseeIndex[$textnode->getLicenseeId()];
90 1
            $obj->importfile = isset($importfileIndex[$textnode->getImportfileId()]) ? $importfileIndex[$textnode->getImportfileId()] : 'unbekannt';
91 1
            $obj->beginning = substr(htmlentities(strip_tags($textnode->getText())), 0, 200)."...";
92 1
            $obj->financenode = $textnode->isFinanceNode() ? 'ja' : 'nein';
93 1
            $obj->twineId = $textnode->getTwineId();
94 1
            $obj->metadata = $this->formatMetadata($textnode->getMetadata());
95 1
            $output[] = $obj;
96
        }
97
98 1
        return new Response(\json_encode($output));
99
    }
100
101 1
    private function buildLicenseeIndex(): array
102
    {
103 1
        $licensees = $this->licenseeRepository->findAll();
104 1
        $index = [];
105 1
        foreach ($licensees as $licensee) {
106 1
            $index[$licensee->getId()] = $licensee->getName();
107
        }
108
109 1
        return $index;
110
    }
111
112 1
    private function formatMetadata(array $metadata): string
113
    {
114 1
        $string = '';
115 1
        foreach ($metadata as $key => $value) {
116 1
            $string .= $key.': '.$value."\n";
117
        }
118
119 1
        return $string;
120
    }
121
122 1
    private function buildImportfileIndex(): array
123
    {
124
        /* @var $importfiles \DembeloMain\Document\Importfile[] */
125 1
        $importfiles = $this->importfileRepository->findAll();
126 1
        $index = [];
127 1
        foreach ($importfiles as $importfile) {
128 1
            $index[$importfile->getID()] = $importfile->getName();
129
        }
130
131 1
        return $index;
132
    }
133
}
134