Passed
Push — master ( dd4450...9a9614 )
by Andrey
05:55
created

Order::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 22
rs 9.7333
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\base\Model;
7
8
/**
9
 * @package app\models
10
 */
11
class Order extends Model
12
{
13
    /**
14
     * @var string
15
     */
16
    public $name;
17
18
    /**
19
     * @var string
20
     */
21
    public $email;
22
23
    /**
24
     * @var string
25
     */
26
    public $phone;
27
28
    /**
29
     * @var string
30
     */
31
    public $comment;
32
33
    /**
34
     * @var array
35
     */
36
    public $quantity = [];
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function rules()
42
    {
43
        return [
44
            [
45
                [
46
                    'name',
47
                    'email',
48
                    'quantity'
49
                ],
50
                'required'
51
            ],
52
            [
53
                'quantity',
54
                'each',
55
                'rule' => ['integer'],
56
            ],
57
            [
58
                [
59
                    'name',
60
                    'email',
61
                    'phone',
62
                ],
63
                'string',
64
                'max' => 64
65
            ],
66
            [
67
                'comment',
68
                'string',
69
                'max' => 2048
70
            ],
71
            [
72
                'email',
73
                'email'
74
            ],
75
        ];
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function attributeLabels()
82
    {
83
        return [
84
            'name' => Yii::t('order', 'Name'),
85
            'email' => Yii::t('order', 'Email'),
86
            'phone' => Yii::t('order', 'Phone'),
87
            'comment' => Yii::t('order', 'Comment'),
88
        ];
89
    }
90
91
    /**
92
     * @param $email
93
     * @return bool
94
     */
95
    public function handle($email)
96
    {
97
        if (!$this->validate()) {
98
            return false;
99
        }
100
101
        $products = Product::find()->where(['in', 'id', array_keys($this->quantity)])->select(['id', 'alias', 'title', 'price'])->all();
102
103
        return Yii::$app->mailer->compose('order', [
104
            'header' => Yii::t('order', 'New client'),
105
            'name' => $this->name,
106
            'email' => $this->email,
107
            'phone' => $this->phone,
108
            'comment' => $this->comment,
109
            'counts' => $this->quantity,
110
            'products' => $products,
111
            'baseUrl' => Yii::$app->homeUrl
112
        ])
113
            ->setTo($email)
114
            ->setFrom([$this->email => $this->name])
115
            ->setSubject(Yii::t('order', 'New order in {project_name} site.', ['project_name' => Yii::$app->params['project_name']]))
116
            ->send();
117
    }
118
}
119