Completed
Push — master ( 8edd81...6e79a7 )
by David
05:02
created

TagManagerSmarty   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getDbSelect() 0 38 8
A toString() 0 4 1
1
<?php
2
/**
3
 * Tag Manager
4
 * Copyright (c) Webmatch GmbH
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 */
16
17
namespace WbmTagManager\Services;
18
19
use Doctrine\DBAL\Connection;
20
21
class TagManagerSmarty implements TagManagerSmartyInterface
22
{
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * @param Connection $connection
30
     */
31
    public function __construct(Connection $connection)
32
    {
33
        $this->connection = $connection;
34
    }
35
36
    /**
37
     * @param $arguments
38
     *
39
     * @return string
40
     *
41
     * @throws \Exception
42
     */
43
    public function getDbSelect($arguments)
44
    {
45
        if (
46
            empty($arguments['select']) ||
47
            empty($arguments['from'])
48
        ) {
49
            return "";
50
        }
51
52
        $qb = $this->connection->createQueryBuilder();
53
54
        $qb->select($arguments['select'])
55
            ->from($arguments['from']);
56
57
        if (is_array($arguments['where'])) {
58
            foreach ($arguments['where'] as $column => $value) {
59
                $qb->andWhere(
60
                    sprintf(
61
                        '%s %s',
62
                        $column,
63
                        $qb->createNamedParameter($value)
64
                    )
65
                );
66
            }
67
        }
68
69
        if (is_array($arguments['order'])) {
70
            foreach ($arguments['order'] as $column => $order) {
71
                $qb->addOrderBy($column, $order);
72
            }
73
        }
74
75
        try {
76
            return $qb->execute()->fetch(\PDO::FETCH_COLUMN);
77
        } catch (\Exception $exception) {
78
            return "";
79
        }
80
    }
81
82
    /**
83
     * @param mixed $value
84
     *
85
     * @return string
86
     */
87
    public function toString($value)
88
    {
89
        return sprintf('\"%s\"', $value);
90
    }
91
}
92