Completed
Pull Request — master (#2)
by Ronaldo
01:43
created

Fractal::getFractal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace WSW\SiftScience\Support\Traits\Transformers;
4
5
use League\Fractal\Manager;
6
use League\Fractal\Resource\Collection;
7
use League\Fractal\Resource\Item;
8
use WSW\SiftScience\Support\Serializers\SiftScienceSerializer;
9
use WSW\SiftScience\Transformers\AbstractTransformer;
10
11
/**
12
 * Trait Fractal
13
 *
14
 * @package WSW\SiftScience\Support\Traits\Transformers
15
 * @author Ronaldo Matos Rodrigues <[email protected]>
16
 */
17
trait Fractal
18
{
19
    /**
20
     * @var \League\Fractal\Manager
21
     */
22
    private $fractal;
23
24
    /**
25
     * @return \League\Fractal\Manager
26
     */
27 19
    public function getFractal()
28
    {
29 19
        if (!$this->fractal instanceof Manager) {
30 19
            $this->fractal = new Manager();
31 19
            $this->fractal->setSerializer(new SiftScienceSerializer);
32
        }
33
34 19
        return $this->fractal;
35
    }
36
37
    /**
38
     * @param null $data
39
     * @param \WSW\SiftScience\Transformers\AbstractTransformer $transformer
40
     *
41
     * @return \League\Fractal\Scope
42
     */
43 16
    public function loadItem($data = null, AbstractTransformer $transformer = null)
44
    {
45 16
        return $this->getFractal()->createData(new Item($data, $transformer));
46
    }
47
48
    /**
49
     * @param null $data
50
     * @param \WSW\SiftScience\Transformers\AbstractTransformer $transformer
51
     *
52
     * @return \League\Fractal\Scope
53
     */
54 12
    public function loadCollection($data = null, AbstractTransformer $transformer = null)
55
    {
56 12
        return $this->getFractal()->createData(new Collection($data, $transformer));
57
    }
58
}
59