Completed
Push — archetype ( c64050...c8605f )
by Arnaud
02:30
created

NewPage::processCommand()   B

Complexity

Conditions 7
Paths 68

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.4106
c 0
b 0
f 0
cc 7
nc 68
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 Symfony\Component\Process\Process;
14
use Zend\Console\Prompt\Confirm;
15
16
class NewPage extends AbstractCommand
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $name;
22
    /**
23
     * @var bool
24
     */
25
    protected $force;
26
27
    public function processCommand()
28
    {
29
        $this->name = $this->getRoute()->getMatchedParam('name');
30
        $this->force = $this->getRoute()->getMatchedParam('force', false);
31
32
        try {
33
            // file name (without extension)
34
            if (false !== $extPos = strripos($this->name, '.md')) {
35
                $this->name = substr($this->name, 0, $extPos);
36
            }
37
            // path
38
            $fileRelativePath = $this->getPHPoole()->getConfig()->get('content.dir').'/'.$this->name.'.md';
39
            $filePath = $this->getPath().'/'.$fileRelativePath;
40
41
            // file already exists?
42
            if ($this->fs->exists($filePath) && !$this->force) {
43
                if (!Confirm::prompt('This page already exists. Do you want to override it? [y/n]', 'y', 'n')) {
44
                    exit(0);
45
                }
46
            }
47
48
            // create new file
49
            $title = $this->name;
50
            if (false !== strrchr($this->name, '/')) {
51
                $title = substr(strrchr($this->name, '/'), 1);
52
            }
53
            $date = date('Y-m-d');
54
            $fileContent = str_replace(['%title%', '%date%'], [$title, $date], $this->findArchetype($this->name));
55
            $this->fs->dumpFile($filePath, $fileContent);
56
            $this->wlDone(sprintf('File "%s" created!', $fileRelativePath));
57
            // open editor?
58
            $this->openEditor($filePath);
59
        } catch (\Exception $e) {
60
            throw new \Exception(sprintf($e->getMessage()));
61
        }
62
    }
63
64
    /**
65
     * Find the archtetype and return its content.
66
     *
67
     * @param string $name
68
     *
69
     * @return string
70
     */
71
    protected function findArchetype($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73
        $section = strstr($this->name, '/', true);
74
        if ($section && file_exists($archetype = sprintf('%s/archetypes/%s.md', $this->getPath(), $section))) {
75
            return file_get_contents($archetype);
76
        }
77
        if (file_exists($archetype = sprintf('%s/archetypes/default.md', $this->getPath()))) {
78
            return file_get_contents($archetype);
79
        }
80
        return <<<'EOT'
81
---
82
title: '%title%'
83
date: '%date%'
84
draft: true
85
---
86
87
EOT;
88
    }
89
90
    /**
91
     * Open new file in editor (if configured).
92
     *
93
     * @param string $filePath
94
     *
95
     * @return void
96
     */
97
    protected function openEditor($filePath)
98
    {
99
        if ($editor = $this->phpoole->getConfig()->get('editor')) {
100
            $command = sprintf('%s %s', $editor, $filePath);
101
            $process = new Process($command);
102
            $process->run();
103
            if (!$process->isSuccessful()) {
104
                throw new \Exception(sprintf("Can't open '%s' editor.", $editor));
105
            }
106
        }
107
    }
108
}
109