Completed
Push — master ( c4a3c9...95b644 )
by Nikola
03:44
created

ReplicatedDestination::push()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 22
loc 22
rs 9.2
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Destination;
14
15
use RunOpenCode\Backup\Contract\BackupInterface;
16
use RunOpenCode\Backup\Contract\DestinationInterface;
17
use RunOpenCode\Backup\Log\LoggerAwareTrait;
18
19
/**
20
 * Class ReplicatedDestination
21
 *
22
 * Replicated destination enables to you to backup on one master and one slave destination where failed backup attempt
23
 * to slave destination will not fail backup process.
24
 *
25
 * @package RunOpenCode\Backup\Destination
26
 */
27
final class ReplicatedDestination implements DestinationInterface
28
{
29
30
    use LoggerAwareTrait;
31
32
    /**
33
     * @var DestinationInterface
34
     */
35
    private $master;
36
37
    /**
38
     * @var DestinationInterface
39
     */
40
    private $slave;
41
42
    /**
43
     * @var bool
44
     */
45
    private $atomic;
46
47
    public function __construct(DestinationInterface $master, DestinationInterface $slave, $atomic = false)
48
    {
49
        $this->master = $master;
50
        $this->slave = $slave;
51
        $this->atomic = $atomic;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 View Code Duplication
    public function push(BackupInterface $backup)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $this->master->push($backup);
60
61
        try {
62
            $this->slave->push($backup);
63
        } catch (\Exception $e) {
64
65
            if ($this->atomic) {
66
                throw $e;
67
            } else {
68
69
                $this->getLogger()->error('Unable to backup to slave destination.', array(
70
                    'message' => $e->getMessage(),
71
                    'code' => $e->getCode(),
72
                    'file' => $e->getFile(),
73
                    'line' => $e->getLine(),
74
                    'trace' => $e->getTrace()
75
                ));
76
            }
77
        }
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function get($key)
84
    {
85
        return $this->master->get($key);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function has($key)
92
    {
93
       return $this->master->has($key);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 View Code Duplication
    public function delete($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $this->master->delete($key);
102
103
        try {
104
            $this->slave->delete($key);
105
        } catch (\Exception $e) {
106
107
            if ($this->atomic) {
108
                throw $e;
109
            } else {
110
111
                $this->getLogger()->error(sprintf('Unable to delete backyp "%s" from slave destination.', $key), array(
112
                    'message' => $e->getMessage(),
113
                    'code' => $e->getCode(),
114
                    'file' => $e->getFile(),
115
                    'line' => $e->getLine(),
116
                    'trace' => $e->getTrace()
117
                ));
118
            }
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function all()
126
    {
127
        return $this->master->all();
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getIterator()
134
    {
135
        return $this->master->getIterator();
136
    }
137
}