Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
44.44% covered (danger)
44.44%
36 / 81
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
LinkableType
44.44% covered (danger)
44.44%
36 / 81
14.29% covered (danger)
14.29%
1 / 7
258.22
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLinkedFormField
71.43% covered (warning)
71.43%
35 / 49
0.00% covered (danger)
0.00%
0 / 1
18.57
 formatRequestLabel
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 formatInactiveObjectLabel
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 formatStatusObjectLabel
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 formatProofLabel
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 formatViolationLabel
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Application\Form\Type;
4
5use App\Application\Interfaces\CollectivityRelated;
6use App\Domain\Registry\Dictionary\MesurementStatusDictionary;
7use App\Domain\Registry\Dictionary\ProofTypeDictionary;
8use App\Domain\Registry\Dictionary\ViolationNatureDictionary;
9use App\Domain\Registry\Model\Mesurement;
10use App\Domain\Registry\Model\Proof;
11use App\Domain\Registry\Model\Request;
12use App\Domain\Registry\Model\Treatment;
13use App\Domain\Registry\Model\Violation;
14use App\Domain\User\Model\User;
15use Doctrine\ORM\EntityRepository;
16use Symfony\Component\Form\AbstractType;
17use Symfony\Component\Security\Core\Security;
18
19class LinkableType extends AbstractType
20{
21    private Security $security;
22
23    public function __construct(Security $security)
24    {
25        $this->security = $security;
26    }
27
28    protected function getLinkedFormField(string $label, string $class, $object, string $ariaLabel = '', $orderField = null): array
29    {
30        return [
31            'label'         => $label,
32            'class'         => $class,
33            'required'      => false,
34            'multiple'      => true,
35            'expanded'      => false,
36            'by_reference'  => false,
37            'query_builder' => function (EntityRepository $er) use ($object, $orderField, $class) {
38                if ($object instanceof CollectivityRelated && !\is_null($object->getCollectivity())) {
39                    $collectivity = $object->getCollectivity();
40                } else {
41                    /** @var User $authenticatedUser */
42                    $authenticatedUser = $this->security->getUser();
43                    $collectivity      = $authenticatedUser->getCollectivity();
44                }
45                $qb = $er->createQueryBuilder('c');
46                if (isset($orderField) && is_array($orderField)) {
47                    foreach ($orderField as $field => $dir) {
48                        $qb->addOrderBy('c.' . $field, $dir);
49                    }
50                } elseif ($orderField) {
51                    $qb->addOrderBy('c.' . $orderField, 'asc');
52                }
53                if ($object instanceof $class) {
54                    // self referencing, remove current object from query
55                    $qb->andWhere('c != :self')
56                        ->setParameter('self', $object)
57                    ;
58                }
59                if ($collectivity) {
60                    return $qb
61                        ->andWhere('c.collectivity = :collectivity')
62                        ->setParameter('collectivity', $collectivity)
63                    ;
64                }
65
66                return $qb;
67            },
68            'attr' => [
69                'class'            => 'selectpicker',
70                'data-live-search' => 'true',
71                'title'            => 'global.placeholder.multiple_select',
72                'aria-label'       => $ariaLabel,
73            ],
74            'choice_label' => function ($object) {
75                if ($object instanceof Request) {
76                    return $this->formatRequestLabel($object);
77                }
78                if ($object instanceof Violation) {
79                    return $this->formatViolationLabel($object);
80                }
81                if ($object instanceof Proof) {
82                    return $this->formatProofLabel($object);
83                }
84                if ($object instanceof Treatment) {
85                    return $this->formatInactiveObjectLabel($object);
86                }
87                if ($object instanceof Mesurement) {
88                    return $this->formatStatusObjectLabel($object);
89                }
90
91                return $object->__toString();
92            },
93        ];
94    }
95
96    protected function formatRequestLabel(Request $object)
97    {
98        if (!\method_exists($object, '__toString')) {
99            throw new \RuntimeException('The object ' . \get_class($object) . ' must implement __toString() method');
100        }
101
102        if (\method_exists($object, 'getDeletedAt') && null !== $object->getDeletedAt()) {
103            return '(Archivé) ' . $object->getDate()->format('d/m/Y') . ' - ' . $object->__toString();
104        }
105
106        return $object->getDate()->format('d/m/Y') . ' - ' . $object->__toString();
107    }
108
109    /**
110     * Prefix every inactive object with "Inactif".
111     */
112    protected function formatInactiveObjectLabel($object): string
113    {
114        if (!\method_exists($object, '__toString')) {
115            throw new \RuntimeException('The object ' . \get_class($object) . ' must implement __toString() method');
116        }
117
118        if (\method_exists($object, 'isActive') && !$object->isActive()) {
119            return '(Inactif) ' . $object->__toString();
120        }
121
122        return $object->__toString();
123    }
124
125    /**
126     * Add status to object name.
127     */
128    protected function formatStatusObjectLabel($object): string
129    {
130        if (!\method_exists($object, '__toString')) {
131            throw new \RuntimeException('The object ' . \get_class($object) . ' must implement __toString() method');
132        }
133
134        if (\method_exists($object, 'getStatus')) {
135            return $object->__toString() . ' (' . MesurementStatusDictionary::getStatus()[$object->getStatus()] . ')';
136        }
137
138        return $object->__toString();
139    }
140
141    /**
142     * Prefix every proof.
143     */
144    protected function formatProofLabel(Proof $object): string
145    {
146        if (!\method_exists($object, '__toString')) {
147            throw new \RuntimeException('The object ' . \get_class($object) . ' must implement __toString() method');
148        }
149
150        if (\method_exists($object, 'getDeletedAt') && null !== $object->getDeletedAt()) {
151            return '(Archivé) ' . $object->__toString() . ' (' . ProofTypeDictionary::getTypes()[$object->getType()] . ')';
152        }
153
154        return $object->__toString() . ' (' . ProofTypeDictionary::getTypes()[$object->getType()] . ')';
155    }
156
157    /**
158     * Prefix violations.
159     */
160    protected function formatViolationLabel(Violation $object): string
161    {
162        if (!\method_exists($object, '__toString')) {
163            throw new \RuntimeException('The object ' . \get_class($object) . ' must implement __toString() method');
164        }
165
166        $natures = [];
167
168        if ($object->getViolationNatures()) {
169            $raw = $object->getViolationNatures();
170            if (is_string($raw)) {
171                $raw = explode(',', $raw);
172            }
173            $natures = array_map(function ($n) {return ViolationNatureDictionary::getNatures()[trim($n)]; }, (array) $raw);
174        }
175
176        if (\method_exists($object, 'getDeletedAt') && null !== $object->getDeletedAt()) {
177            return '(Archivé) ' . $object->getDate()->format('d/m/Y') . ' - ' . join(', ', $natures);
178        }
179
180        return $object->getDate()->format('d/m/Y') . ' - ' . join(', ', $natures);
181    }
182}