Completed
Push — commands ( 4282a7 )
by Arnaud
04:21
created

NewPage::processCommand()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
cc 5
nc 10
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 bool
19
     */
20
    protected $force;
21
22
    public function processCommand()
23
    {
24
        $this->name = $this->getRoute()->getMatchedParam('name');
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->force = $this->getRoute()->getMatchedParam('force', false);
26
27
        try {
28
            $fileContent = <<<EOT
29
---
30
title: '%s'
31
date: '%s'
32
draft: true
33
---
34
# New page
35
EOT;
36
            $filePath = $this->getPath().'/'.$this->getPHPoole()->getConfig()->get('content.dir').'/'.$this->name.'.md';
37
38
            if ($this->fs->exists($filePath) && !$this->force) {
39
                if (!Confirm::prompt('This page already exists. Do you want to override it? [y/n]', 'y', 'n')) {
40
                    exit(0);
41
                }
42
            }
43
            $this->fs->dumpFile($filePath, sprintf($fileContent, $this->name, date('Y-m-d')));
44
            $this->wlDone('Done!');
45
        } catch (\Exception $e) {
46
            throw new \Exception(sprintf($e->getMessage()));
47
        }
48
    }
49
}
50