Test Failed
Push — master ( 24a173...6e5e04 )
by Alexey
03:01
created

LoadSubscribersData::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\DataFixtures\ORM;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Skobkin\Bundle\PointToolsBundle\Entity\Subscription;
9
use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent;
10
use Skobkin\Bundle\PointToolsBundle\Entity\User;
11
12
/**
13
 * Load user subscriptions
14
 */
15
class LoadSubscribersData extends AbstractFixture implements OrderedFixtureInterface
16
{
17
    public function load(ObjectManager $om)
18
    {
19
        /** @var User[] $users */
20
        $users = [
21
            $this->getReference('test_user_99999'),
22
            $this->getReference('test_user_99998'),
23
            $this->getReference('test_user_99997'),
24
            $this->getReference('test_user_99996'),
25
            $this->getReference('test_user_99995'),
26
        ];
27
28
        foreach ($users as $key => $user) {
29
            // At least 2 subscribers for first user in the list
30
            if (0 === $key) {
31
                $minimum = 2;
32
            } else {
33
                $minimum = mt_rand(0, count($users));
34
            }
35
36
            $subscribers = $this->getRandomSubscribers($users, $minimum);
37
38
            foreach ($subscribers as $subscriber) {
39
                $subscription = new Subscription($user,$subscriber);
40
                $subscriptionEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_SUBSCRIBE);
41
                $om->persist($subscription);
42
                $om->persist($subscriptionEvent);
43
                $user->addSubscriber($subscription);
44
            }
45
        }
46
47
        $om->flush();
48
    }
49
50
    public function getOrder()
51
    {
52
        return 4;
53
    }
54
55
    /**
56
     * Returns array with random users from given users array
57
     *
58
     * @param User[] $users
59
     * @param int $min
60
     *
61
     * @return User[]
62
     */
63
    private function getRandomSubscribers($users, $min = 0)
64
    {
65
        if (0 === $number = mt_rand($min, count($users))) {
66
            return [];
67
        }
68
69
        $keys = array_rand($users, $number);
70
71
        $result = [];
72
73
        foreach ($keys as $key) {
74
            $result[] = $users[$key];
75
        }
76
77
        return $result;
78
    }
79
}