Passed
Pull Request — master (#213)
by Ruslan
18:45
created

MenuItemCompanyWidget::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Pyz\Yves\CompanyWidget\Widget;
9
10
use Spryker\Yves\Kernel\Widget\AbstractWidget;
11
12
/**
13
 * @method \SprykerShop\Yves\CompanyWidget\CompanyWidgetFactory getFactory()
14
 */
15
class MenuItemCompanyWidget extends AbstractWidget
16
{
17
    /**
18
     * @var string
19
     */
20
    protected const PARAMETER_IS_VISIBLE = 'isVisible';
21
22
    /**
23
     * @var string
24
     */
25
    protected const PARAMETER_COMPANY_NAME = 'companyName';
26
27
    /**
28
     * @var string
29
     */
30
    protected const PARAMETER_HAS_COMPANY_ACCESS = 'hasCompanyAccess';
31
32
    public function __construct()
33
    {
34
        $this->addIsVisibleParameter();
35
        $this->addCompanyNameParameter();
36
        $this->addHasCompanyAccessParameter();
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public static function getName(): string
43
    {
44
        return 'MenuItemCompanyWidget';
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public static function getTemplate(): string
51
    {
52
        return '@CompanyWidget/views/shop-ui/menu-item-company-widget.twig';
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    protected function addIsVisibleParameter(): void
59
    {
60
        $customer = $this->getFactory()->getCustomerClient()->getCustomer();
61
        $isVisible = ($customer !== null && $customer->getCompanyUserTransfer() !== null);
62
63
        $this->addParameter(static::PARAMETER_IS_VISIBLE, $isVisible);
64
    }
65
66
    /**
67
     * @return void
68
     */
69
    protected function addCompanyNameParameter(): void
70
    {
71
        $this->addParameter(static::PARAMETER_COMPANY_NAME, $this->getCompanyName());
72
    }
73
74
    /**
75
     * @return void
76
     */
77
    protected function addHasCompanyAccessParameter(): void
78
    {
79
        $customerTransfer = $this->getFactory()->getCustomerClient()->getCustomer();
80
        $hasCompanyAccess = $customerTransfer && ($customerTransfer->getCompanyUserTransfer() || $customerTransfer->getIsOnBehalf());
81
82
        $this->addParameter(static::PARAMETER_HAS_COMPANY_ACCESS, $hasCompanyAccess);
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    protected function getCompanyName(): string
89
    {
90
        $customer = $this->getFactory()->getCustomerClient()->getCustomer();
91
92
        if (
93
            $customer !== null
94
            && $customer->getCompanyUserTransfer() !== null
95
            && $customer->getCompanyUserTransfer()->getCompanyBusinessUnit() !== null
96
            && $customer->getCompanyUserTransfer()->getCompanyBusinessUnit()->getCompany() !== null
97
        ) {
98
            return $customer->getCompanyUserTransfer()->getCompanyBusinessUnit()->getCompany()->getName();
99
        }
100
101
        return '';
102
    }
103
}
104