Common::saveFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
rs 9.9666
cc 3
nc 3
nop 6
1
<?php
2
3
namespace TgScraper\Commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Logger\ConsoleLogger;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
trait Common
10
{
11
12
    protected function saveFile(
13
        ConsoleLogger $logger,
14
        OutputInterface $output,
15
        string $destination,
16
        string $data,
17
        ?string $prefix = null,
18
        bool $log = true
19
    ): int {
20
        $result = file_put_contents($destination, $data);
21
        if (false === $result) {
22
            $logger->critical($prefix . 'Unable to save file to ' . $destination);
23
            return Command::FAILURE;
24
        }
25
        if ($log) {
26
            $logger->info($prefix . 'Done!');
27
            return Command::SUCCESS;
28
        }
29
        $output->writeln($prefix . 'Done!');
30
        return Command::SUCCESS;
31
    }
32
33
}