UpdateMeetingCommandHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 17 1
B syncAttachments() 0 25 4
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Meetings\Handlers;
29
30
use Angelov\Storgman\Meetings\Attachments\Attachment;
31
use Angelov\Storgman\Meetings\Attachments\Repositories\AttachmentsRepositoryInterface;
32
use Angelov\Storgman\Meetings\Commands\UpdateMeetingCommand;
33
use Angelov\Storgman\Meetings\Events\MeetingWasUpdatedEvent;
34
use Angelov\Storgman\Meetings\Meeting;
35
use Angelov\Storgman\Meetings\Repositories\MeetingsRepositoryInterface;
36
use Illuminate\Contracts\Events\Dispatcher;
37
38
class UpdateMeetingCommandHandler
39
{
40
    protected $meetings;
41
    protected $attachments;
42
    protected $events;
43
44
    public function __construct(MeetingsRepositoryInterface $meetings, AttachmentsRepositoryInterface $attachments, Dispatcher $events)
45
    {
46
        $this->meetings = $meetings;
47
        $this->attachments = $attachments;
48
        $this->events = $events;
49
    }
50
51
    public function handle(UpdateMeetingCommand $command)
52
    {
53
        $meeting = $this->meetings->get($command->getMeetingId());
54
55
        $meeting->setTitle($command->getTitle());
56
        $meeting->setDate(new \DateTime($command->getDate()));
57
        $meeting->setLocation($command->getLocation());
58
        $meeting->setInfo($command->getDetails());
59
60
        $attachments = $this->attachments->getByIds($command->getAttachments());
61
62
        $this->syncAttachments($meeting, $attachments);
63
64
        $this->meetings->store($meeting);
65
66
        $this->events->fire(new MeetingWasUpdatedEvent($meeting));
67
    }
68
69
    /**
70
     * @param Meeting $meeting
71
     * @param Attachment[] $attachments
72
     */
73
    private function syncAttachments(Meeting $meeting, array $attachments)
74
    {
75
        $existing = $meeting->getAttachments();
76
        $current = [];
77
78
        foreach ($attachments as $attachment) {
79
            $id = $attachment->getId();
80
            $current[$id] = $attachment;
81
        }
82
83
        foreach ($existing as $attachment) {
84
            $id = $attachment->getId();
85
86
            if (in_array($id, array_keys($current))) {
87
                unset($current[$id]);
88
                continue;
89
            }
90
91
            $attachment->setMeeting(null);
92
93
            $this->attachments->store($attachment);
94
        }
95
96
        $meeting->addAttachments($current);
97
    }
98
}
99