Completed
Pull Request — master (#2)
by Tim
03:25
created

getEntityIdVisibilityIdMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
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
26
/**
27
 * The subject implementation that handles the business logic to persist URL rewrites.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product-url-rewrite
33
 * @link      http://www.techdivision.com
34
 */
35
class UrlRewriteSubject extends AbstractProductSubject
36
{
37
38
    /**
39
     * The mapping for the SKU => visibility.
40
     *
41
     * @var array
42
     */
43
    protected $entityIdVisibilityIdMapping = array();
44
45
    /**
46
     * The array with the available visibility keys.
47
     *
48
     * @var array
49
     */
50
    protected $availableVisibilities = array(
51
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
52
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
53
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
54
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
55
    );
56
57
    /**
58
     * Return's the visibility key for the passed visibility string.
59
     *
60
     * @param string $visibility The visibility string to return the key for
61
     *
62
     * @return integer The requested visibility key
63
     * @throws \Exception Is thrown, if the requested visibility is not available
64
     */
65
    public function getVisibilityIdByValue($visibility)
66
    {
67
68
        // query whether or not, the requested visibility is available
69
        if (isset($this->availableVisibilities[$visibility])) {
70
            // load the visibility ID, add the mapping and return the ID
71
            return $this->availableVisibilities[$visibility];
72
        }
73
74
        // throw an exception, if not
75
        throw new \Exception(
76
            $this->appendExceptionSuffix(
77
                sprintf('Found invalid visibility %s', $visibility)
78
            )
79
        );
80
    }
81
82
    /**
83
     * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created
84
     * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will
85
     * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>.
86
     *
87
     * @return integer The visibility ID
88
     * @throws \Exception Is thrown, if the entity ID has not been mapped
89
     * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue()
90
     */
91
    public function getEntityIdVisibilityIdMapping()
92
    {
93
94
        // query whether or not the SKU has already been mapped to it's visibility
95
        if (isset($this->entityIdVisibilityIdMapping[$entityId = $this->getLastEntityId()])) {
96
            return $this->entityIdVisibilityIdMapping[$entityId];
97
        }
98
99
        // throw a new exception
100
        throw new \Exception(
101
            $this->appendExceptionSuffix(
102
                sprintf('Can\'t find visibility mapping for entity ID "%d"', $entityId)
103
            )
104
        );
105
    }
106
107
    /**
108
     * Add the entity ID => visibility mapping for the actual entity ID.
109
     *
110
     * @param string $visibility The visibility of the actual entity to map
111
     *
112
     * @return void
113
     */
114
    public function addEntityIdVisibilityIdMapping($visibility)
115
    {
116
        $this->entityIdVisibilityIdMapping[$this->getLastEntityId()] = $this->getVisibilityIdByValue($visibility);
117
    }
118
}
119