Completed
Pull Request — master (#443)
by
unknown
02:03
created

ImportCommandController::prepareEvents()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.4906
c 0
b 0
f 0
cc 7
nc 22
nop 2
1
<?php
2
3
/**
4
 * Import.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Command;
9
10
use HDNET\Calendarize\Service\ICalServiceInterface;
11
use HDNET\Calendarize\Service\IndexerService;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\SymfonyStyle;
17
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Core\Utility\MathUtility;
20
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
21
22
/**
23
 * Import.
24
 */
25
class ImportCommandController extends Command
26
{
27
28
    /**
29
     * @var ICalServiceInterface $iCalService
30
     */
31
    private $iCalService;
32
33
    /**
34
     * ImportCommandController constructor.
35
     * @param ICalServiceInterface $iCalService
36
     */
37
    public function __construct(string $name = null, ICalServiceInterface $iCalService)
38
    {
39
        $this->iCalService = $iCalService;
40
        parent::__construct($name);
41
    }
42
43
    protected function configure()
44
    {
45
        $this->setDescription('Imports a iCalendar ICS into a page ID')
46
            ->addArgument('icsCalendarUri', InputArgument::REQUIRED, 'The URI of the iCalendar ICS')
47
            ->addArgument('pid', InputArgument::REQUIRED, 'The page ID to create new elements');
48
    }
49
50
    /**
51
     * Executes the command for importing a iCalendar ICS into a page ID.
52
     *
53
     * @param InputInterface $input
54
     * @param OutputInterface $output
55
     * @return int 0 if everything went fine, or an exit code
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $io = new SymfonyStyle($input, $output);
60
61
        $icsCalendarUri = $input->getArgument('icsCalendarUri');
62
        $pid = $input->getArgument('pid');
63
64
        if (!\filter_var($icsCalendarUri, FILTER_VALIDATE_URL)) {
65
            $io->error('You have to enter a valid URL to the iCalendar ICS');
66
67
            return 1;
68
        }
69
        if (!MathUtility::canBeInterpretedAsInteger($pid)) {
70
            $io->error('You have to enter a valid PID for the new created elements');
71
72
            return 2;
73
        }
74
        $pid = (int)$pid;
75
76
        // fetch external URI and write to file
77
        $io->section('Start to checkout the calendar: ' . $icsCalendarUri);
78
        $relativeIcalFile = 'typo3temp/ical.' . GeneralUtility::shortMD5($icsCalendarUri) . '.ical';
0 ignored issues
show
Bug introduced by
It seems like $icsCalendarUri defined by $input->getArgument('icsCalendarUri') on line 61 can also be of type array<integer,string> or null; however, TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79
        $absoluteIcalFile = GeneralUtility::getFileAbsFileName($relativeIcalFile);
80
        $content = GeneralUtility::getUrl($icsCalendarUri);
0 ignored issues
show
Bug introduced by
It seems like $icsCalendarUri defined by $input->getArgument('icsCalendarUri') on line 61 can also be of type array<integer,string> or null; however, TYPO3\CMS\Core\Utility\GeneralUtility::getUrl() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
        GeneralUtility::writeFile($absoluteIcalFile, $content);
82
83
        $events = $this->iCalService->getEvents($absoluteIcalFile);
84
85
        $io->text('Found ' . \count($events) . ' events in ' . $icsCalendarUri);
86
87
        $signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class);
88
89
        $io->section('Send the ' . __CLASS__ . '::importCommand signal for each event.');
90
        $io->progressStart(\count($events));
91
        foreach ($events as $event) {
92
            $arguments = [
93
                'event' => $event,
94
                'io' => $io,
95
                'pid' => $pid,
96
                'handled' => false,
97
            ];
98
            $signalSlotDispatcher->dispatch(__CLASS__, 'importCommand', $arguments);
99
            $io->progressAdvance();
100
        }
101
        $io->progressFinish();
102
103
        $io->section('Run Reindex proces after import');
104
        $indexer = GeneralUtility::makeInstance(IndexerService::class);
105
        $indexer->reindexAll();
106
107
        return 0;
108
    }
109
110
}
111