Completed
Push — master ( 66885a...b2aa2c )
by David
03:41
created

TagManagerSmarty   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getDbSelect() 0 35 8
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
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
            $i = 0;
59
            foreach ($arguments['where'] as $column => $value) {
60
                $qb->andWhere(sprintf('%s :value%s', $column, $i));
61
                $qb->setParameter(sprintf('value%s', $i), $value);
62
                $i++;
63
            }
64
        }
65
66
        if (is_array($arguments['order'])) {
67
            foreach ($arguments['order'] as $column => $order) {
68
                $qb->addOrderBy($column, $order);
69
            }
70
        }
71
72
        try {
73
            return $qb->execute()->fetch(\PDO::FETCH_COLUMN);
74
        } catch (\Exception $exception) {
75
            return "";
76
        }
77
    }
78
}
79