Completed
Push — build-options ( 5749a9 )
by Arnaud
04:10
created

NewPage::processCommand()   B

Complexity

Conditions 7
Paths 52

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
cc 7
nc 52
nop 0
1
<?php
2
/*
3
 * This file is part of the PHPoole package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
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 PHPoole\Command;
12
13
use Zend\Console\Prompt\Confirm;
14
15
class NewPage extends AbstractCommand
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $name;
21
    /**
22
     * @var bool
23
     */
24
    protected $force;
25
26
    public function processCommand()
27
    {
28
        $this->name = $this->getRoute()->getMatchedParam('name');
29
        $this->force = $this->getRoute()->getMatchedParam('force', false);
30
31
        try {
32
            $fileContent = <<<'EOT'
33
---
34
title: '%s'
35
date: '%s'
36
draft: true
37
---
38
# New page
39
EOT;
40
            if (false !== $extPos = strripos($this->name, '.md')) {
41
                $this->name = substr($this->name, 0, $extPos);
42
            }
43
            $fileRelativePath = $this->getPHPoole()->getConfig()->get('content.dir').'/'.$this->name.'.md';
44
            $filePath = $this->getPath().'/'.$fileRelativePath;
45
            if ($this->fs->exists($filePath) && !$this->force) {
46
                if (!Confirm::prompt('This page already exists. Do you want to override it? [y/n]', 'y', 'n')) {
47
                    exit(0);
48
                }
49
            }
50
            $title = $this->name;
51
            if (false !== strrchr($this->name, '/')) {
52
                $title = substr(strrchr($this->name, '/'), 1);
53
            }
54
            $date = date('Y-m-d');
55
            $this->fs->dumpFile($filePath, sprintf($fileContent, $title, $date));
56
            $this->wlDone(sprintf('File "%s" created!', $fileRelativePath));
57
        } catch (\Exception $e) {
58
            throw new \Exception(sprintf($e->getMessage()));
59
        }
60
    }
61
}
62