Completed
Pull Request — master (#6)
by Dima
21:31
created

Gallery::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 19
rs 9.2
cc 4
eloc 7
nc 3
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: myslyvyi
5
 * Date: 05.01.2016
6
 * Time: 15:25
7
 */
8
namespace samsoncms\api;
9
10
use samson\cms\CMSGallery;
11
use samsoncms\api\MaterialField;
12
13
/***
14
 *  Gallery additional field for material.
15
 *  This class enables getting all information about additional fields gallery for specific material.
16
 */
17
class Gallery
18
{
19
    /** @var integer materialFieldId Table materialField identifier */
20
    protected $materialFieldId = null;
21
22
    /** @var QueryInterface Database query interface */
23
    protected $query;
24
25
    /**
26
     * Constructor Gallery.
27
     * This constructor finds identifier additional field gallery from database record its material and field identifiers.
28
     *
29
     * @param QueryInterface $query Database query interface
30
     * @param integer $materialId material identifier
31
     * @param integer $fieldId field identifier
32
     */
33
    public function __construct(QueryInterface $query, $materialId, $fieldId)
34
    {
35
        /** @var object $materialField additional field value database record*/
36
        $materialField = null;
37
38
        //set query interface
39
        $this->query = $query;
40
41
        // Checking params by type
42
        if (is_int($materialId) && is_int($fieldId)) {
43
            //Find additional field value database record by its material and field identifiers.
44
            if (MaterialField::byFieldIDAndMaterialID($query, $materialId, $fieldId, $materialField)) {
45
                //Getting first record
46
                $materialField = array_shift($materialField);
47
                //Set materialFieldId
48
                $this->materialFieldId = $materialField->id;
49
            }
50
        }
51
    }
52
53
    /**
54
     * Check on empty gallery.
55
     *
56
     * @return boolean
57
     **/
58
    public function hasImages()
59
    {
60
        return (isset($this->materialFieldId));
61
    }
62
63
    /**
64
     * Get collection of images for material by gallery additional field selector. If none is passed
65
     * all images from gallery table would be returned empty array.
66
     *
67
     * @return array
68
     */
69
    public function getImages()
70
    {
71
        /** @var $images[] Get material images for this gallery */
72
        $images = array();
73
74
        if ($this->hasImages()) {
75
            //Get All images for materialFieldId
76
            $images = $this->query
77
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
78
                ->cond(Field::F_DELETION, 1)
79
                ->where(MaterialField::F_PRIMARY, $this->materialFieldId)
80
                ->exec();
81
        }
82
83
        return $images;
84
    }
85
}