FileRename   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
dl 0
loc 35
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 4
A execute() 0 8 2
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
}