Completed
Push — master ( 9a3a3c...24148b )
by Michael
14s
created

SnapshotCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 104
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 104
loc 104
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A doExecute() 42 42 4
A configure() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer\Commands;
10
11
use Joomla\Console\Command\AbstractCommand;
12
use Joomla\StatsServer\Repositories\StatisticsRepository;
13
use Joomla\StatsServer\Views\Stats\StatsJsonView;
14
use League\Flysystem\Filesystem;
15
use Symfony\Component\Console\Exception\InvalidOptionException;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
21
/**
22
 * Command to take a record snapshot
23
 */
24 View Code Duplication
class SnapshotCommand extends AbstractCommand
0 ignored issues
show
Duplication introduced by
This class 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...
25
{
26
	/**
27
	 * The default command name
28
	 *
29
	 * @var  string|null
30
	 */
31
	protected static $defaultName = 'snapshot';
32
33
	/**
34
	 * JSON view for displaying the statistics.
35
	 *
36
	 * @var  StatsJsonView
37
	 */
38
	private $view;
39
40
	/**
41
	 * Filesystem adapter for the snapshots space.
42
	 *
43
	 * @var  Filesystem
44
	 */
45
	private $filesystem;
46
47
	/**
48
	 * Constructor.
49
	 *
50
	 * @param   StatsJsonView  $view        JSON view for displaying the statistics.
51
	 * @param   Filesystem     $filesystem  Filesystem adapter for the snapshots space.
52
	 */
53 3
	public function __construct(StatsJsonView $view, Filesystem $filesystem)
54
	{
55 3
		$this->view       = $view;
56 3
		$this->filesystem = $filesystem;
57
58 3
		parent::__construct();
59 3
	}
60
61
	/**
62
	 * Internal function to execute the command.
63
	 *
64
	 * @param   InputInterface   $input   The input to inject into the command.
65
	 * @param   OutputInterface  $output  The output to inject into the command.
66
	 *
67
	 * @return  integer  The command exit code
68
	 */
69 3
	protected function doExecute(InputInterface $input, OutputInterface $output): int
70
	{
71 3
		$symfonyStyle = new SymfonyStyle($input, $output);
72
73 3
		$symfonyStyle->title('Creating Statistics Snapshot');
74
75
		// We want the full raw data set for our snapshot
76 3
		$this->view->isAuthorizedRaw(true);
77
78 3
		$source = $input->getOption('source');
79
80 3
		$filename = date('YmdHis');
81
82 3
		if ($source)
83
		{
84 2
			if (!\in_array($source, StatisticsRepository::ALLOWED_SOURCES))
85
			{
86 1
				throw new InvalidOptionException(
87 1
					\sprintf(
88 1
						'Invalid source "%s" given, valid options are: %s',
89 1
						$source,
90 1
						implode(', ', StatisticsRepository::ALLOWED_SOURCES)
91
					)
92
				);
93
			}
94
95 1
			$this->view->setSource($source);
96
97 1
			$filename .= '_' . $source;
98
		}
99
100 2
		if (!$this->filesystem->write($filename, $this->view->render()))
101
		{
102
			$symfonyStyle->error('Failed writing snapshot to the filesystem.');
103
104
			return 1;
105
		}
106
107 2
		$symfonyStyle->success('Snapshot recorded.');
108
109 2
		return 0;
110
	}
111
112
	/**
113
	 * Configures the current command.
114
	 *
115
	 * @return  void
116
	 */
117 3
	protected function configure(): void
118
	{
119 3
		$this->setDescription('Takes a snapshot of the statistics data.');
120 3
		$this->addOption(
121 3
			'source',
122 3
			null,
123 3
			InputOption::VALUE_OPTIONAL,
124 3
			'If given, filters the snapshot to a single source.'
125
		);
126 3
	}
127
}
128