Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Site / StatisticsCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Site\StatisticsCommand.
6  */
7
8 namespace Drupal\Console\Command\Site;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
14 use Drupal\Console\Core\Style\DrupalStyle;
15 use Drupal\Console\Utils\DrupalApi;
16 use Drupal\Core\Entity\Query\QueryFactory;
17 use Drupal\Console\Extension\Manager;
18 use Drupal\Core\Extension\ModuleHandlerInterface;
19
20 /**
21  * Class StatisticsCommand
22  *
23  * @package Drupal\Console\Command\Site
24  */
25 class StatisticsCommand extends Command
26 {
27     use ContainerAwareCommandTrait;
28
29     /**
30      * @var DrupalApi
31      */
32     protected $drupalApi;
33
34     /**
35      * @var QueryFactory
36      */
37     protected $entityQuery;
38
39     /**
40      * @var Manager
41      */
42     protected $extensionManager;
43
44     /**
45      * @var ModuleHandlerInterface
46      */
47     protected $moduleHandler;
48
49     /**
50      * StatisticsCommand constructor.
51      *
52      * @param DrupalApi              $drupalApi
53      * @param QueryFactory           $entityQuery;
54      * @param Manager                $extensionManager
55      * @param ModuleHandlerInterface $moduleHandler
56      */
57     public function __construct(
58         DrupalApi $drupalApi,
59         QueryFactory $entityQuery,
60         Manager $extensionManager,
61         ModuleHandlerInterface $moduleHandler
62     ) {
63         $this->drupalApi = $drupalApi;
64         $this->entityQuery = $entityQuery;
65         $this->extensionManager = $extensionManager;
66         $this->moduleHandler = $moduleHandler;
67         parent::__construct();
68     }
69
70     /**
71      * @{@inheritdoc}
72      */
73     public function configure()
74     {
75         $this
76             ->setName('site:statistics')
77             ->setDescription($this->trans('commands.site.statistics.description'))
78             ->setHelp($this->trans('commands.site.statistics.help'));
79         ;
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     protected function execute(InputInterface $input, OutputInterface $output)
86     {
87         $io = new DrupalStyle($input, $output);
88
89         $bundles = $this->drupalApi->getBundles();
90         foreach ($bundles as $bundleType => $bundleName) {
91             $key = sprintf(
92                 $this->trans('commands.site.statistics.messages.node-type'),
93                 $bundleName
94             );
95             $statistics[$key] = $this->getNodeTypeCount($bundleType);
96         }
97         $statistics[$this->trans('commands.site.statistics.messages.comments')] = $this->getCommentCount();
98         $statistics[$this->trans('commands.site.statistics.messages.vocabulary')] = $this->getTaxonomyVocabularyCount();
99         $statistics[$this->trans('commands.site.statistics.messages.taxonomy-terms')] = $this->getTaxonomyTermCount();
100         $statistics[$this->trans('commands.site.statistics.messages.files')] = $this->getFileCount();
101         $statistics[$this->trans('commands.site.statistics.messages.users')] = $this->getUserCount();
102         $statistics[$this->trans('commands.site.statistics.messages.views')] = $this->getViewCount();
103         $statistics[$this->trans('commands.site.statistics.messages.modules-enabled')] = $this->getModuleCount(true);
104         $statistics[$this->trans('commands.site.statistics.messages.modules-disabled')] = $this->getModuleCount(false);
105         $statistics[$this->trans('commands.site.statistics.messages.themes-enabled')] = $this->getThemeCount(true);
106         $statistics[$this->trans('commands.site.statistics.messages.themes-disabled')] = $this->getThemeCount(false);
107
108         $this->statisticsList($io, $statistics);
109     }
110
111
112     /**
113      * @param $nodeType
114      * @return mixed
115      */
116     private function getNodeTypeCount($nodeType)
117     {
118         $nodesPerType = $this->entityQuery->get('node')->condition('type', $nodeType)->count();
119         $nodes = $nodesPerType->execute();
120
121         return $nodes;
122     }
123
124     /**
125      * @return mixed
126      */
127     private function getCommentCount()
128     {
129         if (!$this->moduleHandler->moduleExists('comment')) {
130             return 0;
131         }
132         
133         $entityQuery = $this->entityQuery->get('comment')->count();
134         $comments = $entityQuery->execute();
135
136         return $comments;
137     }
138
139     /**
140      * @return mixed
141      */
142     private function getTaxonomyVocabularyCount()
143     {
144         $entityQuery = $this->entityQuery->get('taxonomy_vocabulary')->count();
145         $vocabularies = $entityQuery->execute();
146
147         return $vocabularies;
148     }
149
150     /**
151      * @return mixed
152      */
153     private function getTaxonomyTermCount()
154     {
155         $entityQuery = $this->entityQuery->get('taxonomy_term')->count();
156         $terms = $entityQuery->execute();
157
158         return $terms;
159     }
160
161     /**
162      * @return mixed
163      */
164     private function getFileCount()
165     {
166         $entityQuery = $this->entityQuery->get('file')->count();
167         $files = $entityQuery->execute();
168
169         return $files;
170     }
171
172     /**
173      * @return mixed
174      */
175     private function getUserCount()
176     {
177         $entityQuery = $this->entityQuery->get('user')->count();
178         $users = $entityQuery->execute();
179
180         return $users;
181     }
182
183     /**
184      * @param bool|TRUE $status
185      * @return int
186      */
187     private function getModuleCount($status = true)
188     {
189         if ($status) {
190             return count($this->extensionManager->discoverModules()->showCore()->showNoCore()->showInstalled()->getList());
191         }
192
193         return count($this->extensionManager->discoverModules()->showCore()->showNoCore()->showUninstalled()->getList());
194     }
195
196     /**
197      * @param bool|TRUE $status
198      * @return int
199      */
200     private function getThemeCount($status = true)
201     {
202         if ($status) {
203             return count($this->extensionManager->discoverThemes()->showCore()->showNoCore()->showInstalled()->getList());
204         }
205
206         return count($this->extensionManager->discoverThemes()->showCore()->showNoCore()->showUninstalled()->getList());
207     }
208
209     /**
210      * @return mixed
211      */
212     private function getViewCount($status = true, $tag = 'default')
213     {
214         $entityQuery = $this->entityQuery->get('view')->condition('tag', 'default', '<>')->count();
215         $views = $entityQuery->execute();
216
217         return $views;
218     }
219
220     /**
221      * @param DrupalStyle $io
222      * @param mixed       $statistics
223      */
224     private function statisticsList(DrupalStyle $io, $statistics)
225     {
226         $tableHeader =[
227             $this->trans('commands.site.statistics.messages.stat-name'),
228             $this->trans('commands.site.statistics.messages.stat-quantity'),
229         ];
230
231         $tableRows = [];
232         foreach ($statistics as $type => $amount) {
233             $tableRows[] = [
234               $type,
235               $amount
236             ];
237         }
238
239         $io->table($tableHeader, $tableRows);
240     }
241 }