FormHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 25
dl 0
loc 89
rs 10
c 0
b 0
f 0
ccs 29
cts 29
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkCollection() 0 16 4
A onPostHandleRequestWithCollection() 0 13 3
A onPreHandleRequestWithCollection() 0 9 2
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Helper;
13
14
use Countable;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\ORM\EntityManagerInterface;
18
use InvalidArgumentException;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use WBW\Bundle\CoreBundle\Doctrine\ORM\EntityManagerTrait;
21
use WBW\Bundle\CoreBundle\Event\NotificationEvent;
22
use WBW\Bundle\CoreBundle\EventDispatcher\EventDispatcherHelper;
23
use WBW\Bundle\CoreBundle\EventDispatcher\EventDispatcherTrait;
24
use WBW\Library\Symfony\Exception\RedirectResponseException;
25
use WBW\Library\Symfony\Factory\NotificationFactory;
26
27
/**
28
 * Form helper.
29
 *
30
 * @author webeweb <https://github.com/webeweb>
31
 * @package WBW\Bundle\CoreBundle\Helper
32
 */
33
class FormHelper {
34
35
    use EventDispatcherTrait;
36 1
    use EntityManagerTrait;
37 1
38
    /**
39
     * Service name.
40
     *
41
     * @avr string
42
     */
43
    const SERVICE_NAME = "wbw.core.helper.form";
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param EntityManagerInterface $entityManager The entity manager.
49
     * @param EventDispatcherInterface $eventDispatcher The event dispatcher.
50
     */
51
    public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher) {
52 70
        $this->setEventDispatcher($eventDispatcher);
53 70
        $this->setEntityManager($entityManager);
54 70
    }
55 70
56
    /**
57
     * Check a collection.
58
     *
59
     * @param Countable|null $collection The collection.
60
     * @param string $notification The notification.
61
     * @param string $redirectUrl The redirect URL.
62
     * @param int $expected The expected count.
63
     * @return void
64
     * @throws InvalidArgumentException Throws an invalid argument exception if collection is null.
65
     * @throws RedirectResponseException Throws a redirect response exception if the collection count is less than $expected.
66
     */
67
    public function checkCollection($collection, string $notification, string $redirectUrl, int $expected = 1): void {
68 30
69
        if (false === is_array($collection) && false === ($collection instanceof Countable)) {
70 30
            throw new InvalidArgumentException("The collection must be a countable");
71 10
        }
72
        if ($expected <= count($collection)) {
0 ignored issues
show
Bug introduced by
It seems like $collection can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        if ($expected <= count(/** @scrutinizer ignore-type */ $collection)) {
Loading history...
73 20
            return;
74 10
        }
75
76
        $eventName    = NotificationEvent::WARNING;
77 10
        $notification = NotificationFactory::newWarningNotification($notification);
78 10
        $event        = new NotificationEvent($eventName, $notification);
79 10
80
        EventDispatcherHelper::dispatch($this->getEventDispatcher(), $event, $eventName);
81 10
82
        throw new RedirectResponseException($redirectUrl, null);
83 10
    }
84
85
    /**
86
     * On post handle request with collection.
87
     *
88
     * @param Collection $oldCollection The old collection.
89
     * @param Collection $newCollection The new collection.
90
     * @return int Returns the deleted count.
91
     */
92
    public function onPostHandleRequestWithCollection(Collection $oldCollection, Collection $newCollection): int {
93 10
94 10
        $deleted = 0;
95 10
96 10
        foreach ($oldCollection as $current) {
97 10
98
            if (false === $newCollection->contains($current)) {
99 10
                $this->getEntityManager()->remove($current);
100 10
                ++$deleted;
101
            }
102 10
        }
103
104
        return $deleted;
105
    }
106
107
    /**
108
     * On pre handle request with collection.
109
     *
110
     * @param Collection $collection The collection.
111 20
     * @return Collection Returns the cloned collection.
112 20
     */
113 20
    public function onPreHandleRequestWithCollection(Collection $collection): Collection {
114 20
115
        $output = new ArrayCollection();
116 20
117
        foreach ($collection as $current) {
118
            $output->add($current);
119
        }
120
121
        return $output;
122
    }
123
}
124