Completed
Push — master ( 3fcbeb...90dea4 )
by Alex
04:27 queued 02:03
created

CustomFieldsModel::getCustomFieldsTemplateBame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Mezon\Service;
3
4
/**
5
 * Class CustomFieldsModel
6
 *
7
 * @package Service
8
 * @subpackage CustomFieldsModel
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/11/08)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Model for processing custom fields
16
 *
17
 * @author Dodonov A.A.
18
 */
19
class CustomFieldsModel
20
{
21
22
    use \Mezon\PdoCrud\ConnectionTrait;
23
24
    /**
25
     * Table name
26
     */
27
    protected $tableName = '';
28
29
    /**
30
     * Constructor
31
     *
32
     * @param string $tableName
33
     *            name of the table
34
     */
35
    public function __construct(string $tableName)
36
    {
37
        $this->tableName = $tableName;
38
    }
39
40
    /**
41
     * Method returns table name
42
     *
43
     * @return string Table name
44
     */
45
    protected function getCustomFieldsTemplateBame(): string
46
    {
47
        return $this->tableName . '_custom_field';
48
    }
49
50
    /**
51
     * Getting custom fields for object
52
     *
53
     * @param int $objectId
54
     *            Object id
55
     * @param array $filter
56
     *            List of required fields or all
57
     * @return array Result of the fetching
58
     */
59
    public function getCustomFieldsForObject(int $objectId, array $filter = [
60
        '*'
61
    ]): array
62
    {
63
        $result = [];
64
65
        $customFields = $this->getConnection()->select(
66
            '*',
67
            $this->getCustomFieldsTemplateBame(),
68
            'object_id = ' . $objectId);
69
70
        foreach ($customFields as $field) {
71
            $fieldName = \Mezon\Functional\Functional::getField($field, 'field_name');
0 ignored issues
show
Deprecated Code introduced by
The function Mezon\Functional\Functional::getField() has been deprecated: Use \Mezon\Functional\Fetcher::getField instead. Deprecated since 2020-01-21 ( Ignorable by Annotation )

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

71
            $fieldName = /** @scrutinizer ignore-deprecated */ \Mezon\Functional\Functional::getField($field, 'field_name');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
72
73
            // if the field in the list or all fields must be fetched
74
            if (in_array($fieldName, $filter) || in_array('*', $filter)) {
75
                $result[$fieldName] = \Mezon\Functional\Functional::getField($field, 'field_value');
0 ignored issues
show
Deprecated Code introduced by
The function Mezon\Functional\Functional::getField() has been deprecated: Use \Mezon\Functional\Fetcher::getField instead. Deprecated since 2020-01-21 ( Ignorable by Annotation )

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

75
                $result[$fieldName] = /** @scrutinizer ignore-deprecated */ \Mezon\Functional\Functional::getField($field, 'field_value');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
            }
77
        }
78
79
        return $result;
80
    }
81
82
    /**
83
     * Deleting custom fields for object
84
     *
85
     * @param int $objectId
86
     *            Object id
87
     * @param array $filter
88
     *            List of required fields or all
89
     */
90
    public function deleteCustomFieldsForObject(int $objectId, array $filter = [
91
        '1=1'
92
    ])
93
    {
94
        $condition = implode(' AND ', array_merge($filter, [
95
            'object_id = ' . $objectId
96
        ]));
97
98
        $this->getConnection()->delete($this->getCustomFieldsTemplateBame(), $condition);
99
    }
100
101
    /**
102
     * Method sets custom field
103
     *
104
     * @param int $objectId
105
     *            Object id
106
     * @param string $fieldName
107
     *            Field name
108
     * @param string $fieldValue
109
     *            Field value
110
     */
111
    public function setFieldForObject(int $objectId, string $fieldName, string $fieldValue): void
112
    {
113
        $connection = $this->getConnection();
114
115
        $objectId = intval($objectId);
116
        $fieldName = htmlspecialchars($fieldName);
117
        $fieldValue = htmlspecialchars($fieldValue);
118
        $record = [
119
            'field_value' => $fieldValue
120
        ];
121
122
        if (count($this->getCustomFieldsForObject($objectId, [
123
            $fieldName
124
        ])) > 0) {
125
            $connection->update(
126
                $this->getCustomFieldsTemplateBame(),
127
                $record,
128
                'field_name LIKE "' . $fieldName . '" AND object_id = ' . $objectId);
129
        } else {
130
            // in the previous line we have tried to update unexisting field, so create it
131
            $record['field_name'] = $fieldName;
132
            $record['object_id'] = $objectId;
133
            $connection->insert($this->getCustomFieldsTemplateBame(), $record);
134
        }
135
    }
136
137
    /**
138
     * Method fetches custom fields for record
139
     *
140
     * @param array $records
141
     *            List of records
142
     * @return array Transformed records
143
     */
144
    public function getCustomFieldsForRecords(array $records): array
145
    {
146
        foreach ($records as $i => $record) {
147
            $records[$i]['custom'] = $this->getCustomFieldsForObject(\Mezon\Functional\Functional::getField($record, 'id'));
0 ignored issues
show
Bug introduced by
It seems like Mezon\Functional\Functio...getField($record, 'id') can also be of type null; however, parameter $objectId of Mezon\Service\CustomFiel...CustomFieldsForObject() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

147
            $records[$i]['custom'] = $this->getCustomFieldsForObject(/** @scrutinizer ignore-type */ \Mezon\Functional\Functional::getField($record, 'id'));
Loading history...
Deprecated Code introduced by
The function Mezon\Functional\Functional::getField() has been deprecated: Use \Mezon\Functional\Fetcher::getField instead. Deprecated since 2020-01-21 ( Ignorable by Annotation )

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

147
            $records[$i]['custom'] = $this->getCustomFieldsForObject(/** @scrutinizer ignore-deprecated */ \Mezon\Functional\Functional::getField($record, 'id'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
148
        }
149
150
        return $records;
151
    }
152
}
153