Completed
Push — master ( 8260e2...8ffaa6 )
by Nicklas
02:17
created

Post::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Nicklas\Comment\Modules;
4
5
/**
6
 * A database driven model.
7
 */
8
class Post extends ActiveRecordModelExtender
9
{
10
11
        /**
12
     * @var string $tableName name of the database table.
13
     */
14
    protected $tableName = "ramverk1_posts";
15
16
    /**
17
     * Columns in the table.
18
     *
19
     * @var integer $id primary key auto incremented.
20
     */
21
    public $id;
22
    public $user;
23
    public $questionId; # Connection to new Question()
24
25
    public $type; # question/answer
26
    public $text;
27
28
    public $created;
29
30
    public $di;
31
32
33
34
    /**
35
     * Returns post with markdown and gravatar
36
     * @param string $sql
37
     * @param array $param
0 ignored issues
show
Documentation introduced by
There is no parameter named $param. Did you maybe mean $params?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
38
     *
39
     * @return array
40
     */
41 1
    public function getAllPosts($sql, $params)
42
    {
43 1
        $posts = $this->findAllWhere("$sql", $params);
44
45 1
        return array_map(function ($post) {
46
47
            // Get e-mail for Post
48 1
            $user = new User($this->db);
49 1
            $user->find("name", $post->user);
50 1
            $post->img = $this->gravatar($user->email);
51
52
            // Get comments for Post
53 1
            $comment = new Comment($this->db);
54 1
            $post->comments = $comment->getComments("parentId = ?", [$post->id]);
55
56
            // Get text
57 1
            $post->markdown = $this->getMD($post->text);
58
59 1
            return $post;
60 1
        }, $posts);
61
    }
62
63
    /**
64
     * return question/answer, three attributes are set, comments connected to them is an array.
65
     *
66
     * @return object
67
    */
68 1
    public function getPost($sql, $params)
69
    {
70 1
        $post = $this->findWhere("$sql", $params);
71
72
        // Get e-mail for question
73 1
        $user = new User($this->db);
74 1
        $user->find("name", $post->user);
75 1
        $post->img = $this->gravatar($user->email);
76
77
78 1
        $comment = new Comment($this->db);
79
        // Start setting attributes
80 1
        $post->img = $this->gravatar($user->email);
81 1
        $post->markdown = $this->getMD($post->text);
82 1
        $post->comments = $comment->getComments("parentId = ?", [$post->id]);
83
84 1
        return $post;
85
    }
86
87
    /**
88
     * Check if a post belongs to user
89
     *
90
     *
91
     * @return boolean
92
     */
93 View Code Duplication
    public function controlAuthority($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $user = new User();
96
        $user->setDb($this->di->get("db"));
97
        $user->find("name", $name);
98
99
        // IF AUTHORITY == admin, then continue
100
        if ($user->authority != "admin") {
101
            return ($user->name == $this->user);
102
        }
103
        return true;
104
    }
105
}
106