Test Failed
Pull Request — master (#20)
by
unknown
03:27
created

getCustomFieldsByModel()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\CustomFields;
5
6
use Gewaer\Models\CustomFieldsModules;
7
8
/**
9
 * Custom Fields Abstract Class
10
 * @property \Phalcon\Di $di
11
 */
12
abstract class AbstractCustomFieldsModel extends \Baka\Database\ModelCustomFields
13
{
14
    /**
15
     * Get all custom fields of the given object
16
     *
17
     * @param  array  $fields
18
     * @return array
19
     */
20 4
    public function getAllCustomFields(array $fields = [])
21
    {
22
        //We does it only find names in plural? We need to fix this or make a workaroun
23 4
        if (!$models = CustomFieldsModules::findFirstByName($this->getSource())) {
1 ignored issue
show
Bug introduced by
The method findFirstByName() does not exist on Gewaer\Models\CustomFieldsModules. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        if (!$models = CustomFieldsModules::/** @scrutinizer ignore-call */ findFirstByName($this->getSource())) {
Loading history...
24
            return;
25
        }
26
27 4
        $bind = [$this->getId(), $this->di->getApp()->getId(), $models->getId(), $this->di->getUserData()->default_company];
28
29
        // $customFieldsValueTable = $this->getSource() . '_custom_fields';
30 4
        $customFieldsValueTable = $this->getSource() . '_custom_fields';
31
32
        //We are to make a new query to replace old gewaer implementation.
33 4
        $result = $this->getReadConnection()->prepare("SELECT l.{$this->getSource()}_id,
1 ignored issue
show
Bug introduced by
The method prepare() does not exist on Phalcon\Db\AdapterInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Phalcon\Db\Adapter. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $result = $this->getReadConnection()->/** @scrutinizer ignore-call */ prepare("SELECT l.{$this->getSource()}_id,
Loading history...
34
                                               c.id as field_id,
35
                                               c.name,
36
                                               l.value ,
37
                                               c.users_id,
38
                                               l.created_at,
39
                                               l.updated_at
40 4
                                        FROM {$customFieldsValueTable} l,
41
                                             custom_fields c
42
                                        WHERE c.id = l.custom_fields_id
43 4
                                          AND l.{$this->getSource()}_id = ?
44
                                          AND c.apps_id = ?
45
                                          AND c.custom_fields_modules_id = ?
46
                                          AND c.companies_id = ? 
47
                                          AND l.companies_id = c.companies_id");
48
49 4
        $result->execute($bind);
50
51
        // $listOfCustomFields = $result->fetchAll();
52 4
        $listOfCustomFields = [];
53
54 4
        while ($row = $result->fetch(\PDO::FETCH_OBJ)) {
55
            $listOfCustomFields[$row->name] = $row->value;
56
        }
57
58 4
        return $listOfCustomFields;
59
    }
60
61
    /**
62
     * Get all custom fields of the given model
63
     *
64
     * @param  array  $fields
65
     * @return \Phalcon\Mvc\Model
66
     */
67
    public function getCustomFieldsByModel($modelName)
68
    {
69
        if (!$module = CustomFieldsModules::findFirstByName($modelName)) {
70
            return;
71
        }
72
        $allFields = [];
73
        if ($fields = CustomFields::findByModulesId($module->id)->toArray()) {
1 ignored issue
show
Bug introduced by
The method findByModulesId() does not exist on Gewaer\CustomFields\CustomFields. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        if ($fields = CustomFields::/** @scrutinizer ignore-call */ findByModulesId($module->id)->toArray()) {
Loading history...
74
            foreach ($fields as $field) {
75
                array_push($allFields, $field['name']);
76
            }
77
            return $allFields;
78
        }
79
    }
80
}
81