LoadSubscribersData::load()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 26
cp 0
rs 9.408
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
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
            99999 => $this->getReference('test_user_99999'),
22
            99998 => $this->getReference('test_user_99998'),
23
            99997 => $this->getReference('test_user_99997'),
24
            99996 => $this->getReference('test_user_99996'),
25
            99995 => $this->getReference('test_user_99995'),
26
        ];
27
28
        $subscriptions = [
29
            99999 => [99998, 99997, 99996, 99995],
30
            99998 => [99999, 99997],
31
            99997 => [99999],
32
        ];
33
34
        foreach ($users as $key => $user) {
35
            if (array_key_exists($key, $subscriptions)) {
36
                foreach ($subscriptions[$key] as $userId) {
37
                    $subscriber = $users[$userId];
38
                    $subscription = new Subscription($user, $subscriber);
39
                    $subscriptionEvent = new SubscriptionEvent($user, $subscriber, SubscriptionEvent::ACTION_SUBSCRIBE);
40
                    $om->persist($subscription);
41
                    $om->persist($subscriptionEvent);
42
                    $user->addSubscriber($subscription);
43
                }
44
            }
45
        }
46
47
        $om->flush();
48
    }
49
50
    public function getOrder(): int
51
    {
52
        return 4;
53
    }
54
}