Completed
Push — master ( 2820f4...076502 )
by Vítor
16:50
created

CsvExporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * ZfDebugModule. WebUI and Console commands for debugging ZF2 apps.
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
6
 * @copyright 2016 Vítor Brandão <[email protected]>
7
 */
8
9
namespace Noiselabs\ZfDebugModule\Util\Routing;
10
11
use DateTime;
12
use Noiselabs\ZfDebugModule\Package;
13
14
class CsvExporter
15
{
16
    /**
17
     * @var RouteCollection
18
     */
19
    private $routeCollection;
20
21
    /**
22
     * @var string
23
     */
24
    private $outputDir;
25
26
    /**
27
     * CsvExporter constructor.
28
     *
29
     * @param RouteCollection $routeCollection
30
     * @param string          $outputDir
31
     */
32 2
    public function __construct(RouteCollection $routeCollection, $outputDir)
33
    {
34 2
        $this->routeCollection = $routeCollection;
35 2
        $this->outputDir = rtrim($outputDir, '/');
36 2
    }
37
38
    /**
39
     * @return string
40
     */
41 2
    public function export()
42
    {
43 2
        $datetime = DateTime::createFromFormat('U.u', microtime(true));
44 2
        $fileName = sprintf('%s/%s_routes_%s.csv', $this->outputDir, Package::NAME, $datetime->format('Ymd-His.u'));
45
46 2
        $fp = fopen($fileName, 'w');
47 2
        fputcsv($fp, ['Route name', 'URL', 'Controller', 'Action']);
48 2
        foreach ($this->routeCollection->getRoutes() as $route) {
49
            /* @var Route $route */
50 2
            fputcsv($fp, [$route->getName(), $route->getUrl(), $route->getController(), $route->getAction()]);
51 2
        }
52 2
        fclose($fp);
53
54 2
        return $fileName;
55
    }
56
}
57