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

SnapshotRecentlyUpdatedCommand::doExecute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 43

Duplication

Lines 43
Ratio 100 %

Code Coverage

Tests 19
CRAP Score 4.0138

Importance

Changes 0
Metric Value
dl 43
loc 43
ccs 19
cts 21
cp 0.9048
rs 9.232
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4.0138
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 for recently updated records
23
 */
24 View Code Duplication
class SnapshotRecentlyUpdatedCommand 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:recently-updated';
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 3
		$this->view->isRecent(true);
78
79 3
		$source = $input->getOption('source');
80
81 3
		$filename = date('YmdHis') . '_recent';
82
83 3
		if ($source)
84
		{
85 2
			if (!\in_array($source, StatisticsRepository::ALLOWED_SOURCES))
86
			{
87 1
				throw new InvalidOptionException(
88 1
					\sprintf(
89 1
						'Invalid source "%s" given, valid options are: %s',
90 1
						$source,
91 1
						implode(', ', StatisticsRepository::ALLOWED_SOURCES)
92
					)
93
				);
94
			}
95
96 1
			$this->view->setSource($source);
97
98 1
			$filename .= '_' . $source;
99
		}
100
101 2
		if (!$this->filesystem->write($filename, $this->view->render()))
102
		{
103
			$symfonyStyle->error('Failed writing snapshot to the filesystem.');
104
105
			return 1;
106
		}
107
108 2
		$symfonyStyle->success('Snapshot recorded.');
109
110 2
		return 0;
111
	}
112
113
	/**
114
	 * Configures the current command.
115
	 *
116
	 * @return  void
117
	 */
118 3
	protected function configure(): void
119
	{
120 3
		$this->setDescription('Takes a snapshot of the recently updated statistics data.');
121 3
		$this->addOption(
122 3
			'source',
123 3
			null,
124 3
			InputOption::VALUE_OPTIONAL,
125 3
			'If given, filters the snapshot to a single source.'
126
		);
127 3
	}
128
}
129