Passed
Pull Request — master (#3)
by
unknown
02:39
created

Cloner::cloneAndPersist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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