Passed
Push — master ( 6d626d...5de675 )
by Janko
09:20
created

SpacecraftTickManager::setLock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\Spacecraft;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Stu\Component\Game\SemaphoreConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\SemaphoreConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Stu\Module\Control\SemaphoreUtilInterface;
11
use Stu\Module\Logging\LoggerUtilFactoryInterface;
12
use Stu\Module\Logging\LoggerUtilInterface;
13
use Stu\Module\Tick\Lock\LockManagerInterface;
14
use Stu\Module\Tick\Lock\LockTypeEnum;
15
use Stu\Module\Tick\Spacecraft\ManagerComponent\ManagerComponentInterface;
16
17
final class SpacecraftTickManager implements SpacecraftTickManagerInterface
18
{
19
    private LoggerUtilInterface $loggerUtil;
20
21
    /** @param array<ManagerComponentInterface> $components */
22 1
    public function __construct(
23
        private SemaphoreUtilInterface $semaphoreUtil,
24
        private LockManagerInterface $lockManager,
25
        private EntityManagerInterface $entityManager,
26
        private array $components,
27
        LoggerUtilFactoryInterface $loggerUtilFactory
28
    ) {
29 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil(true);
30
    }
31
32
    #[Override]
33
    public function work(bool $doCommit = false): void
34
    {
35
        $this->setLock(1);
36
37
        try {
38
            foreach ($this->components as $component) {
39
                $startTime = microtime(true);
40
                $component->work();
41
42
                if ($this->loggerUtil->doLog()) {
43
                    $endTime = microtime(true);
44
                    $this->loggerUtil->log(sprintf("\t\t%s, seconds: %F", get_class($component), $endTime - $startTime));
45
                }
46
            }
47
48
            if ($doCommit) {
49
                $this->entityManager->flush();
50
                $this->entityManager->commit();
51
            }
52
        } finally {
53
            $this->clearLock(1);
54
        }
55
    }
56
57
    private function setLock(int $batchGroupId): void
58
    {
59
        //main ship sema on
60
        $mainSema = $this->semaphoreUtil->acquireSemaphore(SemaphoreConstants::MAIN_SHIP_SEMAPHORE_KEY);
61
62
        $this->lockManager->setLock($batchGroupId, LockTypeEnum::SHIP_GROUP);
63
64
        //main ship sema off
65
        $this->semaphoreUtil->releaseSemaphore($mainSema);
66
    }
67
68
    private function clearLock(int $batchGroupId): void
69
    {
70
        $this->lockManager->clearLock($batchGroupId, LockTypeEnum::SHIP_GROUP);
71
    }
72
}
73