AbstractServerProduct::getQuantityOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\cart;
12
13
use DateTime;
14
use hipanel\modules\finance\cart\AbstractCartPosition;
15
use hipanel\modules\server\models\Server;
16
use hiqdev\yii2\cart\DontIncrementQuantityWhenAlreadyInCart;
17
use Yii;
18
19
/**
20
 * Class AbstractServerProduct is an abstract cart position for server produce.
21
 */
22
abstract class AbstractServerProduct extends AbstractCartPosition implements DontIncrementQuantityWhenAlreadyInCart
23
{
24
    /**
25
     * @var Server
26
     */
27
    protected $_model;
28
29
    /**
30
     * Number of months for which the server is purchased
31
     * @var array
32
     */
33
    protected $duration = [1, 3, 6, 12];
34
35
    /** {@inheritdoc} */
36
    public function getIcon()
37
    {
38
        return '<i class="fa fa-server"></i>';
39
    }
40
41
    /** {@inheritdoc} */
42
    public function getQuantityOptions()
43
    {
44
        $result = [];
45
        foreach ($this->duration as $n) {
46
            $date = (new DateTime())->add(new \DateInterval("P{$n}M"));
47
48
            $result[$n] = Yii::t('hipanel:server', '{n, plural, one{# month} other{# months}} till {date}', [
49
                'n' => $n,
50
                'date' => Yii::$app->formatter->asDate($date),
51
            ]);
52
        }
53
54
        return $result;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function serializationMap()
61
    {
62
        $parent = parent::serializationMap();
63
        $parent['_model'] = $this->_model;
64
65
        return $parent;
66
    }
67
}
68