FriendshipsContext   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 114
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A weAreNotFriends() 0 2 1
A sheHasnTRespondedYet() 0 2 1
A weAreFriends() 0 6 1
A somebodyIsFriendWithSomebody() 0 11 2
A __construct() 0 6 1
A iHaveSentAFriendshipRequestTo() 0 14 1
A storeFriendshipBetweenUsers() 0 7 1
A iAmFriendWith() 0 6 1
A somebodyWantsUsToBeFriends() 0 14 1
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network 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
 * Donut Social Network 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 Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace AppBundle\FeatureContexts\Setup;
29
30
use Angelov\Donut\Behat\Service\Storage\StorageInterface;
31
use Angelov\Donut\Core\CommandBus\CommandBusInterface;
32
use Angelov\Donut\Core\UuidGenerator\UuidGeneratorInterface;
33
use Angelov\Donut\Friendships\Commands\StoreFriendshipCommand;
34
use Angelov\Donut\Friendships\FriendshipRequests\Commands\SendFriendshipRequestCommand;
35
use Angelov\Donut\Friendships\FriendshipRequests\Repositories\FriendshipRequestsRepositoryInterface;
36
use Angelov\Donut\Users\User;
37
use Behat\Behat\Context\Context;
38
39
class FriendshipsContext implements Context
40
{
41
    private $storage;
42
    private $uuidGenerator;
43
    private $commandBus;
44
    private $friendshipRequests;
45
46
    public function __construct(StorageInterface $storage, UuidGeneratorInterface $uuidGenerator, CommandBusInterface $commandBus, FriendshipRequestsRepositoryInterface $friendshipRequests)
47
    {
48
        $this->storage = $storage;
49
        $this->uuidGenerator = $uuidGenerator;
50
        $this->commandBus = $commandBus;
51
        $this->friendshipRequests = $friendshipRequests;
52
    }
53
54
    /**
55
     * @Given we (also) are friends
56
     */
57
    public function weAreFriends() : void
58
    {
59
        $friend = $this->storage->get('last_created_user');
60
        $current = $this->storage->get('logged_user');
61
62
        $this->storeFriendshipBetweenUsers($friend, $current);
63
    }
64
65
    /**
66
     * @Given I am friend with :name
67
     */
68
    public function iAmFriendWith(string $name) : void
69
    {
70
        $current = $this->storage->get('logged_user');
71
        $friend = $this->storage->get('created_user_' . $name);
72
73
        $this->storeFriendshipBetweenUsers($current, $friend);
74
    }
75
76
    /**
77
     * @Given :first is friend with :second
78
     */
79
    public function somebodyIsFriendWithSomebody(string $first, string $second) : void
80
    {
81
        if (in_array($first, ['she', 'he'])) {
82
            $firstUser = $this->storage->get('last_created_user');
83
        } else {
84
            $firstUser = $this->storage->get('created_user_' . $first);
85
        }
86
87
        $secondUser = $this->storage->get('created_user_' . $second);
88
89
        $this->storeFriendshipBetweenUsers($firstUser, $secondUser);
90
    }
91
92
    private function storeFriendshipBetweenUsers(User $first, User $second) : void
93
    {
94
        $id = $this->uuidGenerator->generate();
95
        $this->commandBus->handle(new StoreFriendshipCommand($id, $first->getId(), $second->getId()));
96
97
        $id = $this->uuidGenerator->generate();
98
        $this->commandBus->handle(new StoreFriendshipCommand($id, $second->getId(), $first->getId()));
99
    }
100
101
    /**
102
     * @Given we are not friends
103
     * @Given I am not friend with :name
104
     */
105
    public function weAreNotFriends() : void
106
    {
107
        // nothing to be done here, at least for now
108
    }
109
110
    /**
111
     * @Given :name wants us to be friends
112
     */
113
    public function somebodyWantsUsToBeFriends(string $name) : void
114
    {
115
        /** @var User $friend */
116
        $friend = $this->storage->get('created_user_' . $name);
117
118
        /** @var User $user */
119
        $user = $this->storage->get('logged_user');
120
121
        $id = $this->uuidGenerator->generate();
122
        $this->commandBus->handle(new SendFriendshipRequestCommand($id, $friend->getId(), $user->getId()));
123
124
        $request = $this->friendshipRequests->find($id);
125
126
        $this->storage->set('current_friendship_request', $request);
127
    }
128
129
    /**
130
     * @Given I have sent a friendship request to :name
131
     */
132
    public function iHaveSentAFriendshipRequestTo(string $name) : void
133
    {
134
        /** @var User $friend */
135
        $friend = $this->storage->get('created_user_' . $name);
136
137
        /** @var User $user */
138
        $user = $this->storage->get('logged_user');
139
140
        $id = $this->uuidGenerator->generate();
141
        $this->commandBus->handle(new SendFriendshipRequestCommand($id, $user->getId(), $friend->getId()));
142
143
        $request = $this->friendshipRequests->find($id);
144
145
        $this->storage->set('current_friendship_request', $request);
146
    }
147
148
    /**
149
     * @Given (s)he hasn't responded yet
150
     */
151
    public function sheHasnTRespondedYet() : void
152
    {
153
        // nothing to be done
154
    }
155
}
156