Completed
Push — master ( 8a7993...1a063d )
by Nikola
05:00
created

ReplicatedDestination::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 6
    public function __construct(DestinationInterface $master, DestinationInterface $slave, $atomic = false)
48
    {
49 6
        $this->master = $master;
50 6
        $this->slave = $slave;
51 6
        $this->atomic = $atomic;
52 6
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 6
    public function push(BackupInterface $backup)
58
    {
59 6
        $this->master->push($backup);
60
61
        try {
62 6
            $this->slave->push($backup);
63 5
        } catch (\Exception $e) {
64
65 4
            if ($this->atomic) {
66 2
                throw $e;
67 2
            } elseif ($this->getLogger()) {
68
                $this->getLogger()->error('Unable to backup to slave destination.', array(
69
                    'message' => $e->getMessage(),
70
                    'code' => $e->getCode(),
71
                    'file' => $e->getFile(),
72
                    'line' => $e->getLine(),
73
                    'trace' => $e->getTrace()
74
                ));
75
            }
76
        }
77 4
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function get($name)
83
    {
84
        return $this->master->get($name);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function has($name)
91
    {
92
       return $this->master->has($name);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function delete($name)
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...
99
    {
100
        $this->master->delete($name);
101
102
        try {
103
            $this->slave->delete($name);
104
        } catch (\Exception $e) {
105
106
            if ($this->atomic) {
107
                throw $e;
108
            } elseif ($this->getLogger()) {
109
                $this->getLogger()->error(sprintf('Unable to delete backup "%s" from slave destination.', $name), array(
110
                    'message' => $e->getMessage(),
111
                    'code' => $e->getCode(),
112
                    'file' => $e->getFile(),
113
                    'line' => $e->getLine(),
114
                    'trace' => $e->getTrace()
115
                ));
116
            }
117
        }
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function all()
124
    {
125
        return $this->master->all();
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getIterator()
132
    {
133
        return $this->master->getIterator();
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function count()
140
    {
141
        return $this->master->count();
142
    }
143
}
144