Passed
Push — master ( 0956f0...2da600 )
by Nico
15:29 queued 09:54
created

NewDealsInformation::work()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 41.5732

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 16
nop 0
dl 0
loc 47
ccs 6
cts 31
cp 0.1935
crap 41.5732
rs 8.1795
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\Process;
6
7
use Stu\Orm\Repository\DealsRepositoryInterface;
8
use Stu\Orm\Repository\UserRepositoryInterface;
9
10
final class NewDealsInformation implements ProcessTickHandlerInterface
11
{
12
    private const TRADE_POST_ID = 2;
13
    private const TIME_THRESHOLD_SECONDS = 60;
14
15 1
    public function __construct(
16
        private DealsRepositoryInterface $dealsRepository,
17
        private UserRepositoryInterface $userRepository
18 1
    ) {}
19
20 1
    #[\Override]
21
    public function work(): void
22
    {
23 1
        $currentTime = time();
24 1
        $timeThreshold = $currentTime - self::TIME_THRESHOLD_SECONDS;
25
26 1
        $recentDeals = $this->dealsRepository->getRecentlyStartedDeals($timeThreshold);
27
28 1
        if (empty($recentDeals)) {
29 1
            return;
30
        }
31
32
        $globalDealExists = false;
33
        $factionDeals = [];
34
35
        foreach ($recentDeals as $deal) {
36
            $faction = $deal->getFaction();
37
38
            if ($faction === null) {
39
                $globalDealExists = true;
40
                break;
41
            } else {
42
                $factionDeals[$faction->getId()] = true;
43
            }
44
        }
45
46
        if ($globalDealExists) {
47
            $users = $this->userRepository->getUsersWithActiveLicense(
48
                self::TRADE_POST_ID,
49
                $currentTime
50
            );
51
52
            foreach ($users as $user) {
53
                $user->setDeals(true);
54
                $this->userRepository->save($user);
55
            }
56
        } else {
57
            foreach (array_keys($factionDeals) as $factionId) {
58
                $users = $this->userRepository->getUsersWithActiveLicense(
59
                    self::TRADE_POST_ID,
60
                    $currentTime,
61
                    $factionId
62
                );
63
64
                foreach ($users as $user) {
65
                    $user->setDeals(true);
66
                    $this->userRepository->save($user);
67
                }
68
            }
69
        }
70
    }
71
}
72