Completed
Push — develop ( 2f3e71...243396 )
by David
21s queued 12s
created

AddTreeAssociationHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Handler\Framework;
4
5
use App\Command\Framework\AddTreeAssociationCommand;
6
use App\Entity\Framework\AssociationSubtype;
7
use App\Entity\Framework\LsDoc;
8
use App\Entity\Framework\LsItem;
9
use App\Event\CommandEvent;
10
use App\Event\NotificationEvent;
11
use App\Service\FrameworkService;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
class AddTreeAssociationHandler extends BaseFrameworkHandler
17
{
18
    /**
19
     * @var EntityManagerInterface
20
     */
21
    private $em;
22
23
    public function __construct(ValidatorInterface $validator, FrameworkService $framework, EntityManagerInterface $em)
24
    {
25
        $this->em = $em;
26
        parent::__construct($validator, $framework);
27
    }
28
29
    public function handle(CommandEvent $event, string $eventName, EventDispatcherInterface $dispatcher): void
30
    {
31
        /** @var AddTreeAssociationCommand $command */
32
        $command = $event->getCommand();
33
34
        $allowedSubtypes = $this->em->getRepository(AssociationSubtype::class)->findAll();
35
        $command->setAllowedSubtypes($allowedSubtypes);
36
37
        $this->validate($command, $command);
38
39
        $doc = $command->getDoc();
40
        $type = $command->getType();
41
        $origin = $command->getOrigin();
42
        $dest = $command->getDestination();
43
        $assocGroup = $command->getAssocGroup();
44
        $annotation = $command->getAnnotation();
45
46
        $association = $this->framework->addTreeAssociation($doc, $origin, $type, $dest, $assocGroup, $annotation);
47
        $command->setAssociation($association);
48
49
        $fromTitle = $this->getTitle($association->getOrigin());
50
        $toTitle = $this->getTitle($association->getDestination());
51
        $notification = new NotificationEvent(
52
            'A03',
53
            sprintf('"%s" association added from "%s" to "%s"', $association->getType(), $fromTitle, $toTitle),
54
            $association->getLsDoc(),
55
            [
56
                'assoc-a' => [
57
                    $association,
58
                ],
59
            ]
60
        );
61
        $command->setNotificationEvent($notification);
62
    }
63
64
    protected function getTitle($obj): string
65
    {
66
        if (null === $obj) {
67
            return 'NONE';
68
        }
69
70
        if (\is_string($obj)) {
71
            return $obj;
72
        }
73
74
        if ($obj instanceof LsItem || $obj instanceof LsDoc) {
75
            return $obj->getShortStatement();
76
        }
77
78
        return 'UNKNOWN';
79
    }
80
}
81