Completed
Push — master ( 7a4c9e...09d75e )
by Gianluca
04:00
created

WpPostService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 65
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findOneAlbumByName() 0 7 2
A findOneLyricByTitle() 0 7 2
A findOneArtistByName() 0 7 2
A insertPost() 0 19 4
A insertMeta() 0 13 2
1
<?php
2
namespace App\Service;
3
4
class WpPostService
5
{
6
    public function __construct($pdo)
7
    {
8
        $this->pdo = $pdo;
0 ignored issues
show
Bug introduced by
The property pdo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
9
    }
10
11
    public function findOneAlbumByName($albumName)
12
    {
13
        $sql = 'SELECT * FROM muu_posts WHERE post_title = "%'.$albumName.'%" AND post_type="album";';
14
        foreach ($this->pdo->query($sql) as $res) {
15
            return $res;
16
        }
17
    }
18
19
    public function findOneLyricByTitle($title)
20
    {
21
        $sql = 'SELECT * FROM muu_posts WHERE post_title = "'.$title.'" AND post_type="lyrics";';
22
        foreach ($this->pdo->query($sql) as $res) {
23
            return $res;
24
        }
25
    }
26
27
    public function findOneArtistByName($artistName)
28
    {
29
        $sql = 'SELECT * FROM muu_posts WHERE post_title = "'.$artistName.'" AND post_type="artist";';
30
        foreach ($this->pdo->query($sql) as $res) {
31
            return $res;
32
        }
33
    }
34
35
    public function insertPost($params)
36
    {
37
        $sql = $this->pdo->prepare('
38
            INSERT INTO muu_posts
39
            (post_name, post_author, post_date, post_date_gmt, post_title, post_content, post_status, comment_status, ping_status, post_type)
40
            VALUES (:post_name, 1, :post_date, :post_gmt_date, :post_title, :post_content, :post_status, :comment_status, :ping_status, :post_type)
41
        ');
42
        foreach ($params as $key => $param) {
43
            $sql->bindValue($key, $param);
44
        }
45
        if (!$sql->execute()) {
46
            var_dump($this->pdo->errorInfo());die;
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->pdo->errorInfo()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Coding Style Compatibility introduced by
The method insertPost() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
47
        }
48
        $id = $this->pdo->lastInsertId();
49
        $sql = 'SELECT * FROM muu_posts WHERE ID='.$id;
50
        foreach ($this->pdo->query($sql) as $res) {
51
            return $res;
52
        }
53
    }
54
55
    public function insertMeta($params)
56
    {
57
        $sql = $this->pdo->prepare('
58
            INSERT INTO muu_postmeta
59
            (post_id, meta_key, meta_value)
60
            VALUES (:post_id, :key, :value)
61
        ');
62
        foreach ($params as $key => $param) {
63
            $sql->bindValue($key, $param);
64
        }
65
        $sql->execute();
66
        return $this->pdo->lastInsertId();
67
    }
68
}
69