Completed
Pull Request — master (#2)
by Tim
02:46
created

ChildrenCountPlugin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 114
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 2
A prepareAttributes() 0 13 1
A initializeCategory() 0 7 1
A loadCategory() 0 4 1
A loadCategoryChildrenChildrenCount() 0 4 1
A persistCategory() 0 4 1
A mergeEntity() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Category\Plugins\ChildrenCountPlugin
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-category
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Category\Plugins;
22
23
use TechDivision\Import\Utils\EntityStatus;
24
use TechDivision\Import\Plugins\AbstractPlugin;
25
use TechDivision\Import\Category\Utils\MemberNames;
26
27
/**
28
 * Plugin that updates the categories children count attribute after a successfull category import.
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-category
34
 * @link      http://www.techdivision.com
35
 */
36
class ChildrenCountPlugin extends AbstractPlugin
37
{
38
39
    /**
40
     * Process the plugin functionality.
41
     *
42
     * @return void
43
     * @throws \Exception Is thrown, if the plugin can not be processed
44
     */
45
    public function process()
46
    {
47
48
        // load all available categories
49
        $categories = $this->getImportProcessor()->getCategories();
50
51
        // update the categories children count
52
        foreach ($categories as $category) {
53
            // load the category itself
54
            $this->category = $this->loadCategory($entityId = $category[MemberNames::ENTITY_ID]);
0 ignored issues
show
Bug introduced by
The property category does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
56
            // update the category's children count
57
            $this->persistCategory($this->initializeCategory($this->prepareAttributes()));
58
59
            // write a log message
60
            $this->getSystemLogger()->debug(
61
                sprintf('Successfully updated category with ID %d', $entityId)
62
            );
63
        }
64
    }
65
66
    /**
67
     * Prepare the attributes of the entity that has to be persisted.
68
     *
69
     * @return array The prepared attributes
70
     */
71
    protected function prepareAttributes()
72
    {
73
74
        // initialize the values that has to be updated
75
        $updatedAt = date('Y-m-d H:i:s');
76
        $childrenCount = $this->loadCategoryChildrenChildrenCount($this->category[MemberNames::PATH] . '/%');
77
78
        // prepare and return the array with the updated values
79
        return array(
80
            MemberNames::UPDATED_AT     => $updatedAt,
81
            MemberNames::CHILDREN_COUNT => $childrenCount
82
        );
83
    }
84
85
    /**
86
     * Initialize the category with the passed attributes and returns an instance.
87
     *
88
     * @param array $attr The category attributes
89
     *
90
     * @return array The initialized category
91
     */
92
    protected function initializeCategory(array $attr)
93
    {
94
        return $this->mergeEntity(
95
            $this->category,
96
            $attr
97
        );
98
    }
99
100
    /**
101
     * Return's the category with the passed ID.
102
     *
103
     * @param string $id The ID of the category to return
104
     *
105
     * @return array The category
106
     */
107
    protected function loadCategory($id)
108
    {
109
        return $this->getProcessor()->loadCategory($id);
0 ignored issues
show
Bug introduced by
The method getProcessor() does not exist on TechDivision\Import\Cate...ins\ChildrenCountPlugin. Did you maybe mean process()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
110
    }
111
112
    /**
113
     * Return's the children count of the category with the passed path.
114
     *
115
     * @param string $path The path of the category to count the children for
116
     *
117
     * @return integer The children count of the category with the passed path
118
     */
119
    protected function loadCategoryChildrenChildrenCount($path)
120
    {
121
        return $this->getProcessor()->loadCategoryChildrenChildrenCount($path);
0 ignored issues
show
Bug introduced by
The method getProcessor() does not exist on TechDivision\Import\Cate...ins\ChildrenCountPlugin. Did you maybe mean process()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
122
    }
123
124
    /**
125
     * Persist's the passed category data and return's the ID.
126
     *
127
     * @param array $category The category data to persist
128
     *
129
     * @return string The ID of the persisted entity
130
     */
131
    protected function persistCategory($category)
132
    {
133
        return $this->getProcessor()->persistCategory($category);
0 ignored issues
show
Bug introduced by
The method getProcessor() does not exist on TechDivision\Import\Cate...ins\ChildrenCountPlugin. Did you maybe mean process()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
134
    }
135
136
    /**
137
     * Merge's and return's the entity with the passed attributes and set's the
138
     * status to 'update'.
139
     *
140
     * @param array $entity The entity to merge the attributes into
141
     * @param array $attr   The attributes to be merged
142
     *
143
     * @return array The merged entity
144
     */
145
    protected function mergeEntity(array $entity, array $attr)
146
    {
147
        return array_merge($entity, $attr, array(EntityStatus::MEMBER_NAME => EntityStatus::STATUS_UPDATE));
148
    }
149
}
150