SharedCartDetailsWidget::checkActionsPermission()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 8
nc 1024
nop 2
dl 0
loc 11
rs 3.15
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerShop\Yves\SharedCartWidget\Widget;
9
10
use Generated\Shared\Transfer\CustomerTransfer;
11
use Generated\Shared\Transfer\QuoteTransfer;
12
use Spryker\Client\SharedCart\Plugin\ReadSharedCartPermissionPlugin;
13
use Spryker\Client\SharedCart\Plugin\WriteSharedCartPermissionPlugin;
14
use Spryker\Yves\Kernel\PermissionAwareTrait;
15
use Spryker\Yves\Kernel\Widget\AbstractWidget;
16
17
/**
18
 * @method \SprykerShop\Yves\SharedCartWidget\SharedCartWidgetFactory getFactory()
19
 */
20
class SharedCartDetailsWidget extends AbstractWidget
21
{
22
    use PermissionAwareTrait;
23
24
    /**
25
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
26
     * @param array $actions
27
     * @param string[]|null $widgetList
28
     */
29
    public function __construct(QuoteTransfer $quoteTransfer, array $actions, ?array $widgetList = null)
30
    {
31
        $this->addParameter('cart', $quoteTransfer)
32
            ->addParameter('actions', $this->checkActionsPermission($quoteTransfer, $actions));
33
34
        if ($widgetList) {
35
            /** @deprecated Use global widgets instead. */
36
            $this->addWidgets($widgetList);
37
        }
38
    }
39
40
    /**
41
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
42
     * @param array $actions
43
     *
44
     * @return array
45
     */
46
    protected function checkActionsPermission(QuoteTransfer $quoteTransfer, array $actions): array
47
    {
48
        $writeAllowed = $this->can(WriteSharedCartPermissionPlugin::KEY, $quoteTransfer->getIdQuote());
49
        $viewAllowed = $this->can(ReadSharedCartPermissionPlugin::KEY, $quoteTransfer->getIdQuote()) || $writeAllowed;
50
        $deleteAllowed = $this->isDeleteCartAllowed($quoteTransfer) && $writeAllowed;
51
        $actions['view'] = isset($actions['view']) ? $actions['view'] && $viewAllowed : $viewAllowed;
52
        $actions['update'] = isset($actions['update']) ? $actions['update'] && $writeAllowed : $writeAllowed;
53
        $actions['set_default'] = isset($actions['set_default']) ? $actions['set_default'] && $viewAllowed : $viewAllowed;
54
        $actions['delete'] = isset($actions['delete']) ? $actions['delete'] && $deleteAllowed : $deleteAllowed;
55
56
        return $actions;
57
    }
58
59
    /**
60
     * @param \Generated\Shared\Transfer\QuoteTransfer $currentQuoteTransfer
61
     *
62
     * @return bool
63
     */
64
    protected function isDeleteCartAllowed(QuoteTransfer $currentQuoteTransfer): bool
65
    {
66
        return $this->getFactory()->getSharedCartClient()->isQuoteDeletable($currentQuoteTransfer);
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
71
     * @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
72
     *
73
     * @return bool
74
     */
75
    protected function isQuoteOwner(QuoteTransfer $quoteTransfer, CustomerTransfer $customerTransfer): bool
76
    {
77
        return $customerTransfer->getCustomerReference() === $quoteTransfer->getCustomerReference();
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public static function getName(): string
84
    {
85
        return 'SharedCartDetailsWidget';
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public static function getTemplate(): string
92
    {
93
        return '@SharedCartWidget/views/shared-cart-details/shared-cart-details.twig';
94
    }
95
}
96