Completed
Push — master ( 5bcafe...c5dea9 )
by Tim
13s
created

getEntityIdVisibilityIdMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\UrlRewrite\Subjects\UrlRewriteSubject
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    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-url-rewrite
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\UrlRewrite\Subjects;
22
23
use TechDivision\Import\Product\Utils\VisibilityKeys;
24
use TechDivision\Import\Product\Subjects\AbstractProductSubject;
25
use TechDivision\Import\Product\UrlRewrite\Utils\MemberNames;
26
27
/**
28
 * The subject implementation that handles the business logic to persist URL rewrites.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-url-rewrite
34
 * @link      http://www.techdivision.com
35
 */
36
class UrlRewriteSubject extends AbstractProductSubject
37
{
38
39
    /**
40
     * The mapping for the SKU => visibility.
41
     *
42
     * @var array
43
     */
44
    protected $entityIdVisibilityIdMapping = array();
45
46
    /**
47
     * The array with the available visibility keys.
48
     *
49
     * @var array
50
     */
51
    protected $availableVisibilities = array(
52
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
53
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
54
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
55
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
56
    );
57
58
    /**
59
     * Return's the visibility key for the passed visibility string.
60
     *
61
     * @param string $visibility The visibility string to return the key for
62
     *
63
     * @return integer The requested visibility key
64
     * @throws \Exception Is thrown, if the requested visibility is not available
65
     */
66
    public function getVisibilityIdByValue($visibility)
67
    {
68
69
        // query whether or not, the requested visibility is available
70
        if (isset($this->availableVisibilities[$visibility])) {
71
            // load the visibility ID, add the mapping and return the ID
72
            return $this->availableVisibilities[$visibility];
73
        }
74
75
        // throw an exception, if not
76
        throw new \Exception(
77
            $this->appendExceptionSuffix(
78
                sprintf('Found invalid visibility %s', $visibility)
79
            )
80
        );
81
    }
82
83
    /**
84
     * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created
85
     * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will
86
     * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>.
87
     *
88
     * @return integer The visibility ID
89
     * @throws \Exception Is thrown, if the entity ID has not been mapped
90
     * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue()
91
     */
92 View Code Duplication
    public function getEntityIdVisibilityIdMapping()
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...
93
    {
94
95
        // query whether or not the SKU has already been mapped to it's visibility
96
        if (isset($this->entityIdVisibilityIdMapping[$entityId = $this->getLastEntityId()])) {
97
            return $this->entityIdVisibilityIdMapping[$entityId];
98
        }
99
100
        // throw a new exception
101
        throw new \Exception(
102
            $this->appendExceptionSuffix(
103
                sprintf('Can\'t find visibility mapping for entity ID "%d"', $entityId)
104
            )
105
        );
106
    }
107
108
    /**
109
     * Add the entity ID => visibility mapping for the actual entity ID.
110
     *
111
     * @param string $visibility The visibility of the actual entity to map
112
     *
113
     * @return void
114
     */
115
    public function addEntityIdVisibilityIdMapping($visibility)
116
    {
117
        $this->entityIdVisibilityIdMapping[$this->getLastEntityId()] = $this->getVisibilityIdByValue($visibility);
118
    }
119
120
    /**
121
     * Return's TRUE if the store with the passed code is active, else FALSE.
122
     *
123
     * @param string $storeViewCode The store view code of the store to check for the active flag set to 1
124
     *
125
     * @return boolean TRUE if the store is active, else FALSE
126
     * @throws \Exception Is thrown, if the store with the actual code is not available
127
     */
128 View Code Duplication
    public function storeIsActive($storeViewCode)
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...
129
    {
130
131
        // query whether or not, the requested store is available
132
        if (isset($this->stores[$storeViewCode])) {
133
            return 1 === (integer) $this->stores[$storeViewCode][MemberNames::IS_ACTIVE];
134
        }
135
136
        // throw an exception, if not
137
        throw new \Exception(
138
            $this->appendExceptionSuffix(
139
                sprintf('Found invalid store view code %s', $storeViewCode)
140
            )
141
        );
142
    }
143
}
144