Failed Conditions
Pull Request — master (#2127)
by Janko
10:05
created

TradeProvider::setTemplateVariables()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 31
nc 9
nop 1
dl 0
loc 43
ccs 24
cts 30
cp 0.8
crap 5.2
rs 9.1128
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\View\Provider;
6
7
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...
8
use Stu\Component\Game\GameEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\GameEnum 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\Trade\TradeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Trade\TradeEnum 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\Lib\Session\SessionInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Control\ViewContextTypeEnum;
13
use Stu\Module\Trade\Lib\TradeOfferItem;
14
use Stu\Module\Trade\Lib\TradeOfferItemInterface;
15
use Stu\Orm\Entity\TradeOfferInterface;
16
use Stu\Orm\Repository\CommodityRepositoryInterface;
17
use Stu\Orm\Repository\TradeLicenseRepositoryInterface;
18
use Stu\Orm\Repository\TradeOfferRepositoryInterface;
19
20
final class TradeProvider implements ViewComponentProviderInterface
21
{
22 1
    public function __construct(private TradeLicenseRepositoryInterface $tradeLicenseRepository, private TradeOfferRepositoryInterface $tradeOfferRepository, private CommodityRepositoryInterface $commodityRepository, private SessionInterface $session) {}
23
24 1
    #[Override]
25
    public function setTemplateVariables(GameControllerInterface $game): void
26
    {
27 1
        $user = $game->getUser();
28 1
        $userId = $user->getId();
29
30 1
        $isFilterActive = $game->getViewContext(ViewContextTypeEnum::FILTER_ACTIVE) ?? false;
31
32 1
        $commodityId = null;
33 1
        $postId = null;
34 1
        $dir = TradeEnum::FILTER_COMMODITY_IN_BOTH;
35 1
        if ($isFilterActive) {
36
            if ($this->session->getSessionValue('trade_filter_cid')) {
37
                $commodityId = $this->session->getSessionValue('trade_filter_cid');
38
            }
39
            if ($this->session->getSessionValue('trade_filter_pid')) {
40
                $postId = $this->session->getSessionValue('trade_filter_pid');
41
            }
42
            if ($this->session->getSessionValue('trade_filter_dir')) {
43
                $dir = $this->session->getSessionValue('trade_filter_dir');
44
            }
45
        } else {
46 1
            $this->session->deleteSessionData('trade_filter_cid');
47 1
            $this->session->deleteSessionData('trade_filter_pid');
48 1
            $this->session->deleteSessionData('trade_filter_dir');
49
        }
50
51 1
        $game->setTemplateVar('COMMODITY_ID', $commodityId ?? 0);
52 1
        $game->setTemplateVar('POST_ID', $postId ?? 0);
53
54 1
        $tradeLicenses = $this->tradeLicenseRepository->getLicensesCountbyUser($userId);
55 1
        $game->setTemplateVar('TRADE_LICENSES', $tradeLicenses);
56 1
        $game->setTemplateVar('TRADE_LICENSE_COUNT', count($tradeLicenses));
57
58 1
        $commodityList = $this->commodityRepository->getTradeable();
59 1
        $game->setTemplateVar('SELECTABLE_COMMODITIES', $commodityList);
60
61 1
        $game->setTemplateVar('MAX_TRADE_LICENSE_COUNT', GameEnum::MAX_TRADELICENSE_COUNT);
62 1
        $game->setTemplateVar(
63 1
            'OFFER_LIST',
64 1
            array_map(
65 1
                fn(TradeOfferInterface $tradeOffer): TradeOfferItemInterface => new TradeOfferItem($tradeOffer, $user),
66 1
                $this->tradeOfferRepository->getByUserLicenses($userId, $commodityId, $postId, $dir)
67 1
            )
68 1
        );
69
    }
70
}
71