Passed
Push — master ( cbde3b...f22511 )
by Abishek R
02:33
created

HasCustomFields   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addCustomField() 0 11 3
A getCustomFields() 0 3 1
1
<?php
2
3
4
namespace Greenlyst\BaseCommerce\Traits;
5
6
7
use Greenlyst\BaseCommerce\LogicException;
8
9
trait HasCustomFields
10
{
11
    private $customFields = [];
12
13
    protected abstract function getCustomFieldPrefix(): string;
14
15
    /**
16
     * @param $value
17
     *
18
     * @throws LogicException
19
     */
20
    public function addCustomField($value)
21
    {
22
        if ($this->getCustomFieldPrefix() === '') {
23
            throw LogicException::noCustomFieldPrefixDefined();
24
        }
25
26
        if (count($this->customFields) == 10) {
27
            throw LogicException::only10CustomFieldsAreAllowed();
28
        }
29
30
        $this->customFields[$this->getCustomFieldPrefix() . '_custom_field' . (count($this->customFields) + 1)] = $value;
31
    }
32
33
    public function getCustomFields()
34
    {
35
        return $this->customFields;
36
    }
37
}