FileRename::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
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 FileRename
23
{
24
    private $_file;
25
    private $_title;
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['title']))
34
            throw new BadRequestHttpException('Title of file is not set.');
35
36
        $this->_title = $data['title'];
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->title = $this->_title;
52
53
        if (!$this->_file->save())
54
            throw new BadRequestHttpException('Unable to save file.');
55
56
        return $this->_title;
57
    }
58
}