Cloner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Anfischer\Cloner;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
8
class Cloner
9
{
10
    private $cloneService;
11
    private $persistenceService;
12
13
    /**
14
     * @param CloneServiceInterface $cloneService
15
     * @param PersistenceServiceInterface $persistenceService
16
     */
17 10
    public function __construct(CloneServiceInterface $cloneService, PersistenceServiceInterface $persistenceService)
18
    {
19 10
        $this->cloneService = $cloneService;
20 10
        $this->persistenceService = $persistenceService;
21
    }
22
23
    /**
24
     * Clone a model without persisting it
25
     *
26
     * @param $model
27
     * @return Model
28
     */
29 4
    public function clone($model): Model
30
    {
31 4
        return $this->cloneService->clone($model);
32
    }
33
34
    /**
35
     * Persist a cloned model
36
     *
37
     * @param $model
38
     * @return Model
39
     */
40 4
    public function persist($model): Model
41
    {
42 4
        return $this->persistenceService->persist($model);
43
    }
44
45
    /**
46
     * Clone and persist a model
47
     *
48
     * @param $model
49
     * @return Model
50
     */
51 2
    public function cloneAndPersist($model) : Model
52
    {
53 2
        return $this->persist($this->clone($model));
54
    }
55
56
    /**
57
     * Retrieve the map of original keys to cloned keys
58
     *
59
     * @return Collection
60
     */
61 2
    public function getKeyMap(): Collection
62
    {
63 2
        return $this->cloneService->getKeyMap();
64
    }
65
}
66