FileAlt::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 1
b 0
f 1
cc 4
nc 4
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 04.01.2018
6
 * Time: 10:39
7
 */
8
9
namespace floor12\files\logic;
10
11
12
use floor12\files\models\File;
13
use yii\web\BadRequestHttpException;
14
use yii\web\NotFoundHttpException;
15
16
/**
17
 * Class FileRename
18
 * @package floor12\files\logic
19
 * @property File $_file
20
 * @property string $_title
21
 */
22
class FileAlt
23
{
24
    private $_file;
25
    private $alt;
26
27
    public function __construct(array $data)
28
    {
29
30
        if (!isset($data['id']))
31
            throw new BadRequestHttpException('ID of file is not set.');
32
33
        if (!isset($data['alt']))
34
            throw new BadRequestHttpException('Alt of file is not set.');
35
36
        $this->alt = $data['alt'];
37
38
        $this->_file = File::findOne($data['id']);
39
40
        if (!$this->_file)
41
            throw new NotFoundHttpException('File not found.');
42
43
    }
44
45
    /**
46
     * @return string
47
     * @throws BadRequestHttpException
48
     */
49
    public function execute()
50
    {
51
        $this->_file->alt = $this->alt;
52
53
        if (!$this->_file->save())
54
            throw new BadRequestHttpException('Unable to save file.');
55
56
        return $this->alt;
57
    }
58
}