Completed
Push — master ( dd73dd...675fe0 )
by WEBEWEB
02:20
created

FormHelper::checkCollection()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.3888
c 0
b 0
f 0
cc 5
nc 3
nop 4
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\Common\Persistence\ObjectManager;
18
use InvalidArgumentException;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use WBW\Bundle\CoreBundle\Event\NotificationEvent;
21
use WBW\Bundle\CoreBundle\Event\NotificationEvents;
22
use WBW\Bundle\CoreBundle\EventDispatcher\EventDispatcherHelper;
23
use WBW\Bundle\CoreBundle\Exception\RedirectResponseException;
24
use WBW\Bundle\CoreBundle\Notification\NotificationFactory;
25
use WBW\Bundle\CoreBundle\Service\EventDispatcherTrait;
26
use WBW\Bundle\CoreBundle\Service\ObjectManagerTrait;
27
28
/**
29
 * Form helper.
30
 *
31
 * @author webeweb <https://github.com/webeweb/>
32
 * @package WBW\Bundle\CoreBundle\Helper
33
 */
34
class FormHelper {
35
36
    use EventDispatcherTrait;
37
    use ObjectManagerTrait;
38
39
    /**
40
     * Service name.
41
     *
42
     * @avr string
43
     */
44
    const SERVICE_NAME = "wbw.core.helper.form";
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param ObjectManager $objectManager The object manager.
50
     * @param EventDispatcherInterface $eventDispatcher The event dispatcher.
51
     */
52
    public function __construct(ObjectManager $objectManager, EventDispatcherInterface $eventDispatcher) {
53
        $this->setEventDispatcher($eventDispatcher);
54
        $this->setObjectManager($objectManager);
55
    }
56
57
    /**
58
     * Check a collection.
59
     *
60
     * @param Countable $collection The collection.
61
     * @param string $notification The notification.
62
     * @param string $redirectURL The redirect URL.
63
     * @param int $expected The expected count.
64
     * @throws InvalidArgumentException Throws an invalid argument exception if collection is null.
65
     * @throws RedirectResponseException Throws a redirect response exception if the collection is less than $expected.
66
     */
67
    public function checkCollection($collection, $notification, $redirectURL, $expected = 1) {
68
69
        if (null === $collection || (false === is_array($collection) && false === ($collection instanceof Countable))) {
70
            throw new InvalidArgumentException("The collection must be a countable");
71
        }
72
        if ($expected <= count($collection)) {
73
            return;
74
        }
75
76
        $eventName    = NotificationEvents::NOTIFICATION_WARNING;
77
        $notification = NotificationFactory::newWarningNotification($notification);
78
        $event        = new NotificationEvent($eventName, $notification);
79
80
        EventDispatcherHelper::dispatch($this->getEventDispatcher(), $eventName, $event);
81
82
        throw new RedirectResponseException($redirectURL, null);
83
    }
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) {
93
        $deleted = 0;
94
        foreach ($oldCollection as $current) {
95
            if (true === $newCollection->contains($current)) {
96
                continue;
97
            }
98
            $this->getObjectManager()->remove($current);
99
            ++$deleted;
100
        }
101
        return $deleted;
102
    }
103
104
    /**
105
     * On pre handle request with collection.
106
     *
107
     * @param Collection $collection The collection.
108
     * @return Collection Returns the cloned collection.
109
     */
110
    public function onPreHandleRequestWithCollection(Collection $collection) {
111
        $output = new ArrayCollection();
112
        foreach ($collection as $current) {
113
            $output->add($current);
114
        }
115
        return $output;
116
    }
117
}
118