AddModel::add()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 1
dl 0
loc 56
rs 8.0266
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rudolf\Modules\Galleries\One\Admin;
4
5
use Rudolf\Framework\Model\AdminModel;
6
use Rudolf\Component\Modules\Module;
7
8
class AddModel extends AdminModel
9
{
10
    /**
11
     * Add article.
12
     *
13
     * @param array $f
14
     *
15
     * @return int
16
     * @throws \Exception
17
     */
18
    public function add($f)
19
    {
20
        $userInfo = self::$auth->getUser();
21
        $f['added'] = date('Y-m-d H:i:s');
22
        $f['adder_ID'] = $userInfo['id'];
23
24
        $this->pdo->beginTransaction();
25
26
        $stmt = $this->pdo->prepare("
27
            INSERT INTO {$this->prefix}galleries
28
                (title
29
                , added
30
                , adder_ID
31
                , slug
32
                , thumb_width
33
                , thumb_height)
34
            VALUES
35
                (:title
36
                , :added
37
                , :adder_ID
38
                , :slug
39
                , :thumb_width
40
                , :thumb_height)
41
        ");
42
        $stmt->bindValue(':title', $f['title'], \PDO::PARAM_STR);
43
        $stmt->bindValue(':added', $f['added'], \PDO::PARAM_STR);
44
        $stmt->bindValue(':adder_ID', $f['adder_ID'], \PDO::PARAM_INT);
45
        $stmt->bindValue(':slug', $f['slug'], \PDO::PARAM_STR);
46
        $stmt->bindValue(':thumb_width', $f['thumb_width'] ? $f['thumb_width'] : 209, \PDO::PARAM_INT);
47
        $stmt->bindValue(':thumb_height', $f['thumb_height'] ? $f['thumb_height'] : 157, \PDO::PARAM_INT);
48
49
        if (!$stmt->execute()) {
50
            return false;
51
        }
52
53
        $id = $this->pdo->lastInsertId();
54
        $config = (new Module('galleries'))->getConfig();
55
        $directory = $config['path_root'].'/'.$f['slug'];
56
57
        if (file_exists($directory)) {
58
            $directory = $directory.'-'.$id;
59
            $stmt = $this->pdo->prepare("UPDATE {$this->prefix}galleries SET slug = :slug WHERE id = :id");
60
            $stmt->bindValue(':slug', $f['slug'].'-'.$id, \PDO::PARAM_STR);
61
            $stmt->bindValue(':id', $id, \PDO::PARAM_INT);
62
            $stmt->execute();
63
        }
64
65
        if (!mkdir($directory, 0777, true) && !is_dir($directory)) {
66
            $this->pdo->rollBack();
67
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
68
        }
69
70
        $this->pdo->commit();
71
72
        return $id;
73
    }
74
}
75