Issues (3627)

Form/DataTransformer/EventsToArrayTransformer.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2015 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\WebhookBundle\Form\DataTransformer;
13
14
use Doctrine\ORM\PersistentCollection;
15
use Mautic\WebhookBundle\Entity\Event;
16
use Mautic\WebhookBundle\Entity\Webhook;
17
use Symfony\Component\Form\DataTransformerInterface;
18
19
/**
20
 * Class EventsToArrayTransformer.
21
 */
22
class EventsToArrayTransformer implements DataTransformerInterface
23
{
24
    private $webhook;
25
26
    public function __construct(Webhook $webhook)
27
    {
28
        $this->webhook = $webhook;
29
    }
30
31
    /**
32
     * Convert from the PersistentCollection of Event entities to a simple array.
33
     *
34
     * @return array
35
     */
36
    public function transform($events)
37
    {
38
        $eventArray = [];
39
        foreach ($events as $event) {
40
            $eventArray[] = $event->getEventType();
41
        }
42
43
        return $eventArray;
44
    }
45
46
    /**
47
     * Convert a simple array into a PersistentCollection of Event entities.
48
     *
49
     * @return PersistentCollection
50
     */
51
    public function reverseTransform($submittedArray)
52
    {
53
        // Get a list of existing events and types
54
55
        /** @var PersistentCollection $events */
56
        $events     = $this->webhook->getEvents();
57
        $eventTypes = $events->getKeys();
58
59
        // Check to see what events have been removed
60
        $removed = array_diff($eventTypes, $submittedArray);
61
        if ($removed) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $removed of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
            foreach ($removed as $type) {
63
                $this->webhook->removeEvent($events[$type]);
64
            }
65
        }
66
67
        // Now check to see what events have been added
68
        $added = array_diff($submittedArray, $eventTypes);
69
        if ($added) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $added of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70
            foreach ($added as $type) {
71
                // Create a new entity
72
                $event = new Event();
73
                $event->setWebhook($this->webhook)->setEventType($type);
74
                $events[] = $event;
75
            }
76
        }
77
78
        $this->webhook->setEvents($events);
79
80
        return $events;
81
    }
82
}
83