Test Failed
Pull Request — master (#3)
by leon
07:10
created

FileInstaller::install()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright © Youwe. All rights reserved.
5
 * https://www.youweagency.com
6
 */
7
8
declare(strict_types=1);
9
10
namespace Youwe\Composer;
11
12
use Composer\IO\IOInterface;
13
use Youwe\FileMapping\FileMappingInterface;
14
use Youwe\FileMapping\FileMappingReaderInterface;
15
use SplFileObject;
16
17
class FileInstaller
18
{
19
    /** @var FileMappingReaderInterface */
20
    private $mappingReader;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param FileMappingReaderInterface $mappingReader
26
     */
27
    public function __construct(FileMappingReaderInterface $mappingReader)
28
    {
29
        $this->mappingReader = $mappingReader;
30
    }
31
32
    /**
33
     * Install the deployer files.
34
     *
35
     * @param IOInterface $io
36
     *
37
     * @return void
38
     *
39
     * @SuppressWarnings(PHPMD.ShortVariable)
40
     */
41
    public function install(IOInterface $io)
42
    {
43
        foreach ($this->mappingReader as $mapping) {
44
            if (file_exists($mapping->getDestination())) {
45
                continue;
46
            }
47
48
            $this->installFile($mapping);
49
50
            $io->write(
51
                sprintf(
52
                    '<info>Installed:</info> %s',
53
                    $mapping->getRelativeDestination()
54
                )
55
            );
56
        }
57
    }
58
59
    /**
60
     * Install the given file if it does not exist.
61
     *
62
     * @param FileMappingInterface $mapping
63
     *
64
     * @return void
65
     *
66
     * @SuppressWarnings(PHPMD.ShortVariable)
67
     */
68
    public function installFile(FileMappingInterface $mapping)
69
    {
70
        $inputFile  = new SplFileObject($mapping->getSource(), 'r');
71
        $targetFile = new SplFileObject($mapping->getDestination(), 'w+');
72
73
        foreach ($inputFile as $input) {
74
            $targetFile->fwrite($input);
0 ignored issues
show
Bug introduced by
It seems like $input can also be of type array; however, parameter $data of SplFileObject::fwrite() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            $targetFile->fwrite(/** @scrutinizer ignore-type */ $input);
Loading history...
75
        }
76
    }
77
}
78