FilterTranslatablesHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B __invoke() 0 28 2
A paginate() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Application\Query\Translation;
13
14
use LIN3S\CMSKernel\Application\DataTransformer\Translation\TranslatableDataTransformer;
15
use LIN3S\CMSKernel\Domain\Model\Translation\Locale;
16
use LIN3S\CMSKernel\Domain\Model\Translation\TranslatableRepository;
17
use LIN3S\CMSKernel\Domain\Model\Translation\TranslatableSpecificationFactory;
18
19
/**
20
 * @author Beñat Espiña <[email protected]>
21
 */
22
class FilterTranslatablesHandler
23
{
24
    private $dataTransformer;
25
    private $repository;
26
    private $specificationFactory;
27
    private $totalPerPage;
28
29
    public function __construct(
30
        TranslatableRepository $repository,
31
        TranslatableDataTransformer $dataTransformer,
32
        TranslatableSpecificationFactory $specificationFactory,
33
        $totalPerPage = 10
34
    ) {
35
        $this->repository = $repository;
36
        $this->dataTransformer = $dataTransformer;
37
        $this->specificationFactory = $specificationFactory;
38
        $this->totalPerPage = $totalPerPage;
39
    }
40
41
    public function __invoke(FilterTranslatablesQuery $query)
42
    {
43
        $locale = new Locale($query->locale());
44
45
        $specification = $this->specificationFactory->createFilterTranslatable(
46
            $query->filters(),
47
            $query->page(),
48
            $this->totalPerPage
49
        );
50
        $translatables = $this->repository->query($specification);
51
        $totalTranslatables = $this->repository->count($specification);
52
53
        if (!is_array($translatables)) {
54
            $translatables = [$translatables];
55
        }
56
57
        return array_merge(
58
            [
59
                'translatables' => array_map(function ($translatable) use ($locale) {
60
                    $this->dataTransformer->write($translatable, $locale);
61
62
                    return $this->dataTransformer->read();
63
                }, $translatables),
64
65
            ],
66
            $this->paginate($translatables, $totalTranslatables, $query->page())
67
        );
68
    }
69
70
    private function paginate($translatables, $totalTranslatables, $page)
71
    {
72
        if (null === $page) {
73
            return [];
74
        }
75
76
        return [
77
            'totalTranslatables'        => $totalTranslatables,
78
            'initialTranslatableOfPage' => ($page - 1) * $this->totalPerPage + 1,
79
            'lastTranslatableOfPage'    => ($page - 1) * $this->totalPerPage + count($translatables),
80
            'totalPages'                => (int) ceil($totalTranslatables / $this->totalPerPage),
81
            'currentPage'               => $page,
82
        ];
83
    }
84
}
85