Completed
Pull Request — master (#52)
by Olexandr
03:33
created

EntityTable::findEntityIDs()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 3
eloc 16
nc 4
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: nazarenko
5
 * Date: 22.03.2016
6
 * Time: 11:40
7
 */
8
namespace samsoncms\api\query;
9
10
use samsoncms\api\CMS;
11
use samsoncms\api\Material;
12
use samsoncms\api\NavigationMaterial;
13
use samsonframework\orm\QueryInterface;
14
15
/**
16
 * Class for querying nested entity tables.
17
 *
18
 * @package samsoncms\api\query
19
 */
20
class EntityTable extends Entity
21
{
22
    /** @var int Parent entity identifier */
23
    protected $parentID;
24
25
    /** @var int Parent table structure identifier */
26
    protected $structureID;
27
28
    /**
29
     * Generic constructor.
30
     *
31
     * @param int $structureID Parent table structure identifier
32
     * @param int $parentID Parent entity identifier
33
     * @param QueryInterface $query Database query instance
34
     * @param string $locale Query localization
35
     */
36
    public function __construct($structureID, $parentID, QueryInterface $query = null, $locale = null)
37
    {
38
        $this->structureID = $structureID;
39
        $this->parentID = $parentID;
40
41
        parent::__construct($query, $locale);
42
    }
43
44
    /**
45
     * Prepare entity identifiers.
46
     *
47
     * @param array $entityIDs Collection of identifier for filtering
48
     * @return array Collection of entity identifiers
49
     */
50
    protected function findEntityIDs(array $entityIDs = array())
51
    {
52
        // Get all entities related to structure identifier
53
        $allStructureRelatedEntityIDs = $this->query
54
            ->entity(CMS::MATERIAL_NAVIGATION_RELATION_ENTITY)
55
            ->where(NavigationMaterial::F_STRUCTUREID, $this->structureID)
56
            ->where(NavigationMaterial::F_ACTIVE, true);
57
58
        // Filter by passed identifiers
59
        if (count($entityIDs)) {
60
            $allStructureRelatedEntityIDs->where(NavigationMaterial::F_MATERIALID, $entityIDs);
61
        }
62
63
        $result = array();
64
65
        $allStructureRelatedEntityIDs = $allStructureRelatedEntityIDs->fields(NavigationMaterial::F_MATERIALID);
66
        if (count($allStructureRelatedEntityIDs)) {
67
            // Retrieve nested entity identifiers
68
            $result = parent::findEntityIDs($this->query
69
                ->entity(Material::class)
70
                ->where(Material::F_PARENT, $this->parentID)
71
                ->where(Material::F_PRIMARY, $allStructureRelatedEntityIDs)
72
                ->fields(Material::F_PRIMARY)
73
            );
74
        }
75
76
        return $result;
77
    }
78
}