FileWriter::write()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of sitemap-common.
4
 *
5
 * (c) 2016 Daniele Moraschi
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SiteMap\Output\File;
12
13
use SiteMap\Output\Writer;
14
15
class FileWriter implements Writer
16
{
17
18
    /**
19
     * @var string
20
     */
21
    private $fileName;
22
23
    /**
24
     * FileWriter constructor.
25
     * @param $fileName
26
     */
27
    public function __construct($fileName)
28
    {
29
        $this->fileName = (string)$fileName;
30
    }
31
32
    /**
33
     * @param $fileContent
34
     * @return mixed
35
     */
36
    public function write($fileContent)
37
    {
38
        try {
39
            $success = file_put_contents($this->fileName, $fileContent);
40
            if (false === $success) {
41
                throw new \Exception();
42
            }
43
        } catch (\Exception $e) {
44
            throw new FileWriterException(sprintf('Cannot write the file at location %s', $this->fileName));
45
        }
46
47
        return true;
48
    }
49
}