Fields   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 13
c 1
b 0
f 0
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMatrixFields() 0 11 2
A getMatrixFieldIds() 0 13 2
1
<?php
2
/**
3
 * Spoon plugin for Craft CMS 3.x
4
 *
5
 * Enhance Matrix
6
 *
7
 * @link      https://angell.io
8
 * @copyright Copyright (c) 2018 Angell & Co
9
 */
10
11
namespace angellco\spoon\services;
12
13
use angellco\spoon\Spoon;
14
15
use Craft;
16
use craft\base\Component;
17
use craft\db\Query;
18
19
/**
20
 * Fields
21
 *
22
 * @author    Angell & Co
23
 * @package   Spoon
24
 * @since     3.0.0
25
 */
26
class Fields extends Component
27
{
28
29
    // Private Properties
30
    // =========================================================================
31
32
    private $_matrixFieldIds;
33
34
    // Public Methods
35
    // =========================================================================
36
37
    /**
38
     * Returns an array of all the Matrix field ids
39
     * @return array
40
     */
41
    public function getMatrixFieldIds()
42
    {
43
44
        if (!$this->_matrixFieldIds)
45
        {
46
            $this->_matrixFieldIds = (new Query())
47
                ->select(['id'])
48
                ->from('{{%fields}}')
49
                ->where('type = :type', [':type' => 'craft\fields\Matrix'])
50
                ->column();
51
        }
52
53
        return $this->_matrixFieldIds;
54
55
    }
56
57
    /**
58
     * Returns an array of Matrix fields
59
     * @return array
60
     */
61
    public function getMatrixFields()
62
    {
63
64
        $return = [];
65
66
        foreach ($this->getMatrixFieldIds() as $fieldId)
67
        {
68
            $return[] = \Craft::$app->fields->getFieldById($fieldId);
69
        }
70
71
        return $return;
72
73
    }
74
75
}
76