CanonicalizerListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A canonicalize() 0 11 3
A prePersist() 0 4 1
A preUpdate() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
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 App\EventListener;
13
14
use Doctrine\ORM\Event\LifecycleEventArgs;
15
use Sylius\Component\Customer\Model\CustomerInterface;
16
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
17
use Sylius\Component\User\Model\UserInterface;
18
19
final class CanonicalizerListener
20
{
21
    /**
22
     * @var CanonicalizerInterface
23
     */
24
    private $canonicalizer;
25
26
    /**
27
     * @param CanonicalizerInterface $canonicalizer
28
     */
29
    public function __construct(CanonicalizerInterface $canonicalizer)
30
    {
31
        $this->canonicalizer = $canonicalizer;
32
    }
33
34
    /**
35
     * @param LifecycleEventArgs $event
36
     */
37
    public function canonicalize(LifecycleEventArgs $event): void
38
    {
39
        $item = $event->getEntity();
40
41
        if ($item instanceof CustomerInterface) {
42
            $item->setEmailCanonical($this->canonicalizer->canonicalize($item->getEmail()));
43
        } elseif ($item instanceof UserInterface) {
44
            $item->setUsernameCanonical($this->canonicalizer->canonicalize($item->getUsername()));
45
            $item->setEmailCanonical($this->canonicalizer->canonicalize($item->getEmail()));
46
        }
47
    }
48
49
    /**
50
     * @param LifecycleEventArgs $event
51
     */
52
    public function prePersist(LifecycleEventArgs $event): void
53
    {
54
        $this->canonicalize($event);
55
    }
56
57
    /**
58
     * @param LifecycleEventArgs $event
59
     */
60
    public function preUpdate(LifecycleEventArgs $event): void
61
    {
62
        $this->canonicalize($event);
63
    }
64
}
65