AddModel   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B add() 0 56 7
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