HasThinkImage   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 33
ccs 7
cts 7
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A thinkImage() 0 11 4
1
<?php
2
3
namespace SimpleImageManager\Eloquent;
4
5
use SimpleImageManager\Exceptions\SimpleImageManagerException;
6
7
trait HasThinkImage
8
{
9
    /**
10
     * Resolved images.
11
     *
12
     * @var array
13
     */
14
    protected array $thinkImages = [];
15
16
    /**
17
     * Map: field -> driver
18
     */
19
    abstract public function thinkImagesMap(): array;
20
21
    /**
22
     * Image manager.
23
     *
24
     * @param string $field
25
     *
26
     * @return ThinkImage|null
27
     * @throws SimpleImageManagerException
28
     */
29 6
    public function thinkImage(string $field): ThinkImage
30
    {
31 6
        if (!empty($this->thinkImages[ $field ])) {
32 6
            return $this->thinkImages[ $field ]->setValue($this->{$field});
33
        }
34 6
        $map = $this->thinkImagesMap();
35 6
        if (!array_key_exists($field, $map)) {
36 1
            throw new SimpleImageManagerException("Filed [{$field}]  not present in thinkImagesMap.");
37
        }
38
39 6
        return $this->thinkImages[ $field ] = ($map[ $field ] instanceof ThinkImage) ? $map[ $field ] : new ThinkImage($map[ $field ], $this->{$field});
40
    }
41
}
42