Passed
Pull Request — 2.x (#4)
by Tim
02:37
created

SqlStatementRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 44
dl 0
loc 127
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryKeyUtil() 0 3 1
A load() 0 10 2
A replacePrimaryKeyMemberName() 0 3 1
A __construct() 0 12 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Repositories\SqlStatementRepository
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Klaas-Tido Rühl <[email protected]>
15
 * @author    Tim Wagner <[email protected]>
16
 * @copyright 2019 REFUSiON GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/techdivision/import-product-tier-price
19
 * @link      https://www.techdivision.com
20
 * @link      https://www.refusion.com
21
 */
22
23
namespace TechDivision\Import\Product\TierPrice\Repositories;
24
25
use TechDivision\Import\Product\TierPrice\Utils\SqlStatementKeys;
26
use TechDivision\Import\Utils\PrimaryKeyUtilInterface;
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Utils\PrimaryKeyUtilInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
/**
29
 * Adds statements specifically required for tier price CRUD operations.
30
 *
31
 * @author    Klaas-Tido Rühl <[email protected]>
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 REFUSiON GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-product-tier-price
36
 * @link      https://www.techdivision.com
37
 * @link      https://www.refusion.com
38
 */
39
class SqlStatementRepository extends \TechDivision\Import\Repositories\SqlStatementRepository
40
{
41
42
    /**
43
     * The variable name for the PK.
44
     *
45
     * @var string
46
     */
47
    const PK_MEMBER_NAME = 'pk_member_name';
48
49
    /**
50
     * The primary key util instance.
51
     *
52
     * @var \TechDivision\Import\Utils\PrimaryKeyUtilInterface
53
     */
54
    private $primaryKeyUtil;
55
56
    /**
57
     * The SQL statements.
58
     *
59
     * @var array
60
     */
61
    private $statements = array(
62
        SqlStatementKeys::TIER_PRICES =>
63
            'SELECT *
64
               FROM catalog_product_entity_tier_price',
65
        SqlStatementKeys::TIER_PRICE_BY_PK_AND_ALL_GROUPS_AND_CUSTOMER_GROUP_ID_AND_QTY_AND_WEBSITE_ID =>
66
            'SELECT *
67
               FROM catalog_product_entity_tier_price
68
              WHERE ${' . SqlStatementRepository::PK_MEMBER_NAME . '} = :${' . SqlStatementRepository::PK_MEMBER_NAME . '}
69
                AND all_groups = :all_groups
70
                AND customer_group_id = :customer_group_id
71
                AND qty = :qty
72
                AND website_id = :website_id',
73
        SqlStatementKeys::DELETE_TIER_PRICE =>
74
            'DELETE
75
               FROM catalog_product_entity_tier_price
76
              WHERE value_id = :value_id',
77
        SqlStatementKeys::CREATE_TIER_PRICE =>
78
            'INSERT
79
               INTO catalog_product_entity_tier_price
80
                    (all_groups,
81
                     customer_group_id,
82
                     qty,
83
                     value,
84
                     website_id,
85
                     percentage_value,
86
                     ${' . SqlStatementRepository::PK_MEMBER_NAME . '})
87
             VALUES (:all_groups,
88
                     :customer_group_id,
89
                     :qty,
90
                     :value,
91
                     :website_id,
92
                     :percentage_value,
93
                     :${' . SqlStatementRepository::PK_MEMBER_NAME . '})',
94
        SqlStatementKeys::UPDATE_TIER_PRICE =>
95
            'UPDATE catalog_product_entity_tier_price
96
                SET all_groups = :all_groups,
97
                    customer_group_id = :customer_group_id,
98
                    qty = :qty,
99
                    value = :value,
100
                    website_id = :website_id,
101
                    percentage_value = :percentage_value,
102
                    ${' . SqlStatementRepository::PK_MEMBER_NAME . '} = :${' . SqlStatementRepository::PK_MEMBER_NAME . '}
103
              WHERE value_id = :value_id'
104
    );
105
106
    /**
107
     * Initialize the the SQL statements.
108
     *
109
     * @param \TechDivision\Import\Utils\PrimaryKeyUtilInterface $primaryKeyUtil The primary key util instance
110
     */
111
    public function __construct(PrimaryKeyUtilInterface $primaryKeyUtil)
112
    {
113
114
        // call the parent constructor
115
        parent::__construct();
116
117
        // set the primary key util
118
        $this->primaryKeyUtil = $primaryKeyUtil;
119
120
        // merge the class statements
121
        foreach ($this->statements as $key => $statement) {
122
            $this->preparedStatements[$key] = $this->replacePrimaryKeyMemberName($statement);
123
        }
124
    }
125
126
    /**
127
     * Returns the SQL statement with the passed ID.
128
     *
129
     * @param string $id The ID of the SQL statement to return
130
     *
131
     * @return string The SQL statement
132
     * @throws \Exception Is thrown, if the SQL statement with the passed key cannot be found
133
     */
134
    public function load($id)
135
    {
136
137
        // try to find the SQL statement with the passed key
138
        if (isset($this->preparedStatements[$id])) {
139
            return $this->preparedStatements[$id];
140
        }
141
142
        // throw an exception if NOT available
143
        throw new \Exception(sprintf('Can\'t find SQL statement with ID %s', $id));
144
    }
145
146
    /**
147
     * Returns the primary key util instance.
148
     *
149
     * @return \TechDivision\Import\Utils\PrimaryKeyUtilInterface The primary key util instance
150
     */
151
    private function getPrimaryKeyUtil()
152
    {
153
        return $this->primaryKeyUtil;
154
    }
155
156
    /**
157
     * Replaces the PK member name in the passed SQL statement.
158
     *
159
     * @param string $statement The statement to replace the PK member name for
160
     *
161
     * @return string The statement with the replace PK member name
162
     */
163
    private function replacePrimaryKeyMemberName($statement)
164
    {
165
        return str_replace('${' . SqlStatementRepository::PK_MEMBER_NAME . '}', $this->getPrimaryKeyUtil()->getPrimaryKeyMemberName(), $statement);
166
    }
167
}
168