CreateMeetingCommandHandler::handle()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 8.8571
cc 2
eloc 15
nc 2
nop 1
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\Exceptions\NotOwnerOfAttachmentException;
32
use Angelov\Storgman\Meetings\Attachments\Repositories\AttachmentsRepositoryInterface;
33
use Angelov\Storgman\Meetings\Commands\CreateMeetingCommand;
34
use Angelov\Storgman\Meetings\Events\MeetingWasCreatedEvent;
35
use Angelov\Storgman\Meetings\Meeting;
36
use Angelov\Storgman\Meetings\Repositories\MeetingsRepositoryInterface;
37
use Angelov\Storgman\Members\Member;
38
use Angelov\Storgman\Members\Repositories\MembersRepositoryInterface;
39
use Illuminate\Contracts\Events\Dispatcher;
40
41
class CreateMeetingCommandHandler
42
{
43
    protected $meetings;
44
    protected $members;
45
    protected $attachments;
46
    protected $events;
47
48
    public function __construct(
49
        MeetingsRepositoryInterface $meetings,
50
        MembersRepositoryInterface $members,
51
        AttachmentsRepositoryInterface $attachments,
52
        Dispatcher $events)
53
    {
54
        $this->meetings = $meetings;
55
        $this->members = $members;
56
        $this->attachments = $attachments;
57
        $this->events = $events;
58
    }
59
60
    public function handle(CreateMeetingCommand $command)
61
    {
62
        $meeting = new Meeting();
63
64
        if (($title = $command->getTitle()) != "") {
65
            $meeting->setTitle($title);
66
        }
67
68
        $meeting->setDate(new \DateTime($command->getDate()));
69
        $meeting->setLocation($command->getLocation());
70
71
        $creator = $this->members->get($command->getCreatorId());
72
        $meeting->setCreator($creator);
73
74
        $meeting->setInfo($command->getDetails());
75
76
        $attachments = $this->attachments->getByIds($command->getAttachments());
77
        $this->checkAttachmentOwnership($creator, $attachments);
78
        $meeting->addAttachments($attachments);
79
80
        $this->meetings->store($meeting);
81
82
        $this->events->fire(new MeetingWasCreatedEvent($meeting, $command->getNotifyMembers()));
83
84
        return $meeting;
85
    }
86
87
    /**
88
     * @param Member $creator
89
     * @param Attachment[] $attachments
90
     * @throws NotOwnerOfAttachmentException
91
     */
92
    private function checkAttachmentOwnership(Member $creator, array $attachments)
93
    {
94
        foreach ($attachments as $attachment) {
95
            if ($attachment->getOwner()->getId() != $creator->getId()) {
96
                throw new NotOwnerOfAttachmentException("You must own the attachment to add it to a meeting");
97
            }
98
        }
99
    }
100
}
101