Passed
Branch feature/code-analysis (eb61a8)
by Jonathan
06:01
created

TranslationStatus   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 22
c 1
b 0
f 0
dl 0
loc 49
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 23 2
1
<?php
2
3
/**
4
 * webtrees-mod-translationtool: MyArtJaub Translation Tool Module for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees\Module
7
 * @subpackage TranslationTool
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2020, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\TranslationTool\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\I18N;
18
use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
19
use Fisharebest\Webtrees\Http\ViewResponseTrait;
20
use Fisharebest\Webtrees\Services\ModuleService;
21
use MyArtJaub\Webtrees\Module\TranslationTool\TranslationToolModule;
22
use MyArtJaub\Webtrees\Module\TranslationTool\Model\TranslationsAnalyzer;
23
use MyArtJaub\Webtrees\Module\TranslationTool\Services\SourceCodeService;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
/**
29
 * Request handler for displaying the status of translations in MyArtJaub modules
30
 */
31
class TranslationStatus implements RequestHandlerInterface
32
{
33
    use ViewResponseTrait;
34
    
35
    /** @var TranslationToolModule $module */
36
    private $module;
37
    
38
    /** @var SourceCodeService $sourcecode_service */
39
    private $sourcecode_service;
40
    
41
    /**
42
     * Constructor for TranslationStatus request handler
43
     *
44
     * @param ModuleService $module_service
45
     * @param SourceCodeService $sourcecode_service
46
     */
47
    public function __construct(ModuleService $module_service, SourceCodeService $sourcecode_service)
48
    {
49
        $this->module = $module_service->findByInterface(TranslationToolModule::class)->first();
50
        $this->sourcecode_service = $sourcecode_service;
51
    }
52
    
53
    /**
54
     * {@inheritDoc}
55
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
56
     */
57
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59
        $this->layout = 'layouts/administration';
60
        
61
        if ($this->module === null) {
62
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
63
        }
64
        
65
        $locale = I18N::locale();
66
        $sourceCodePaths = $this->sourcecode_service->sourceCodePaths();
67
        $translation_analyser = new TranslationsAnalyzer(
68
            $this->sourcecode_service,
69
            $sourceCodePaths,
70
            $locale->languageTag()
71
        );
72
        
73
        return $this->viewResponse($this->module->name() . '::status', [
74
            'title'                 =>  $this->module->title(),
75
            'language'              =>  $locale->endonym(),
76
            'source_code_paths'     =>  $sourceCodePaths->flatten()->sort(),
77
            'translations_stats'    =>  $translation_analyser->translationsStatictics(),
78
            'missing_translations'  =>  $translation_analyser->missingTranslations(),
79
            'non_used_translations' =>  $translation_analyser->nonUsedMajTranslations()
80
        ]);
81
    }
82
}
83