Completed
Push — master ( 19e0d8...b3dedc )
by Andrii
08:56 queued 06:45
created

Module::getCart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Cart module for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-cart
7
 * @package   yii2-cart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\cart;
13
14
use Closure;
15
use Yii;
16
use yii\helpers\Url;
17
18
/**
19
 * Cart Module.
20
 *
21
 * Example application configuration:
22
 *
23
 * ```php
24
 * 'modules' => [
25
 *     'cart' => [
26
 *         'class'  => 'hiqdev\yii2\cart\Module',
27
 *     ],
28
 * ],
29
 * ```
30
 */
31
class Module extends \yii\base\Module
32
{
33
    /**
34
     * @var array array of the options that will be passed to [[set]]
35
     */
36
    public $shoppingCartOptions = [];
37
38
    /**
39
     * Cart component ID.
40
     */
41
    const CART_COMPONENT_ID = 'cart';
42
43
    /**
44
     * {@inheritdoc}
45
     * @throws \yii\base\InvalidConfigException
46
     */
47 5
    public function init()
48
    {
49
        parent::init();
50
        if (!$this->has(static::CART_COMPONENT_ID)) {
51
            $this->set(static::CART_COMPONENT_ID, array_merge([
52
                'class' => 'hiqdev\yii2\cart\ShoppingCart',
53
            ], $this->shoppingCartOptions));
54
        }
55
        $this->get(static::CART_COMPONENT_ID)->module = $this;
56
        $this->registerTranslations();
57 5
    }
58
59
    /**
60
     * Registers translations.
61
     * @void
62
     */
63 5
    public function registerTranslations()
64
    {
65 5
        Yii::$app->i18n->translations['cart'] = [
66
            'class'          => 'yii\i18n\PhpMessageSource',
67
            'sourceLanguage' => 'en-US',
68
            'basePath'       => '@hiqdev/yii2/cart/messages',
69
            'fileMap'        => [
70
                'merchant' => 'cart.php',
71
            ],
72
        ];
73 5
    }
74
75
    /**
76
     * @var string the module name
77
     */
78
    public static $name = 'cart';
79
80
    /**
81
     * Finds cart module.
82
     * TODO think of how to find NOT by name.
83
     */
84
    public static function getInstance()
85
    {
86
        return Yii::$app->getModule(static::$name);
87
    }
88
89
    /**
90
     * @throws \yii\base\InvalidConfigException
91
     * @return null|\yz\shoppingcart\ShoppingCart|ShoppingCart
92
     */
93
    public function getCart()
94
    {
95
        return $this->get(static::CART_COMPONENT_ID);
96
    }
97
98 1
    public function createUrl($route = null)
99
    {
100
        $params = is_array($route) ? $route : [$route];
101
        $params[0] = '/' . $this->id . '/' . (strpos($params[0], '/') !== false ? $params[0] : 'cart/' . ($params[0] ?: 'index'));
102
103
        return Url::toRoute($params);
104 1
    }
105
106
    /**
107
     * @var string|array route to the terms of use page, suitable for Url::to().
108
     */
109
    public $termsPage;
110
111
    /**
112
     * @var string|array route to the order page, suitable for Url::to().
113
     */
114
    public $orderPage = ['order'];
115
116
    protected $_orderButton;
117
118
    public function setOrderButton($value)
119
    {
120
        $this->_orderButton = $value;
121
    }
122
123
    public function getOrderButton()
124
    {
125
        return $this->_orderButton instanceof Closure ? call_user_func($this->_orderButton, $this) : $this->_orderButton;
126
    }
127
128
    protected $_paymentMethods;
129
130
    public function setPaymentMethods($value)
131
    {
132
        $this->_paymentMethods = $value;
133
    }
134
135
    public function getPaymentMethods()
136
    {
137
        return $this->_paymentMethods instanceof Closure ? call_user_func($this->_paymentMethods, $this) : $this->_paymentMethods;
138
    }
139
}
140