Completed
Push — commands ( 87b0b0 )
by Arnaud
02:11
created

NewPage::findModel()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of the Cecil/Cecil 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 Cecil\Command;
12
13
use Symfony\Component\Process\Process;
14
use Zend\Console\Prompt\Confirm;
15
16
/**
17
 * Class NewPage.
18
 */
19
class NewPage extends AbstractCommand
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $name;
25
    /**
26
     * @var bool
27
     */
28
    protected $force;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function processCommand()
34
    {
35
        $this->name = $this->getRoute()->getMatchedParam('name');
36
        $this->force = $this->getRoute()->getMatchedParam('force', false);
37
38
        try {
39
            // file name (without extension)
40
            if (false !== $extPos = strripos($this->name, '.md')) {
41
                $this->name = substr($this->name, 0, $extPos);
42
            }
43
            // path
44
            $fileRelativePath = $this->getBuilder()->getConfig()->get('content.dir').'/'.$this->name.'.md';
45
            $filePath = $this->getPath().'/'.$fileRelativePath;
46
47
            // file already exists?
48
            if ($this->fs->exists($filePath) && !$this->force) {
49
                if (!Confirm::prompt('This page already exists. Do you want to override it? [y/n]', 'y', 'n')) {
50
                    exit(0);
51
                }
52
            }
53
54
            // create new file
55
            $title = $this->name;
56
            if (false !== strrchr($this->name, '/')) {
57
                $title = substr(strrchr($this->name, '/'), 1);
58
            }
59
            $date = date('Y-m-d');
60
            $fileContent = str_replace(['%title%', '%date%'], [$title, $date], $this->findModel($this->name));
61
            $this->fs->dumpFile($filePath, $fileContent);
62
            $this->wlDone(sprintf('File "%s" created!', $fileRelativePath));
63
            // open editor?
64
            $this->openEditor($filePath);
65
        } catch (\Exception $e) {
66
            throw new \Exception(sprintf($e->getMessage()));
67
        }
68
    }
69
70
    /**
71
     * Find the page model and return its content.
72
     *
73
     * @param string $name
74
     *
75
     * @return string
76
     */
77
    protected function findModel($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...
78
    {
79
        $section = strstr($this->name, '/', true);
80
        if ($section && file_exists($model = sprintf('%s/models/%s.md', $this->getPath(), $section))) {
81
            return file_get_contents($model);
82
        }
83
        if (file_exists($model = sprintf('%s/models/default.md', $this->getPath()))) {
84
            return file_get_contents($model);
85
        }
86
87
        return <<<'EOT'
88
---
89
title: '%title%'
90
date: '%date%'
91
draft: true
92
---
93
94
_[Your content here]_
95
EOT;
96
    }
97
98
    /**
99
     * Open new file in editor (if configured).
100
     *
101
     * @param string $filePath
102
     *
103
     * @return void
104
     */
105
    protected function openEditor($filePath)
106
    {
107
        if ($editor = $this->builder->getConfig()->get('editor')) {
108
            $command = sprintf('%s %s', $editor, $filePath);
109
            $process = new Process($command);
110
            $process->run();
111
            if (!$process->isSuccessful()) {
112
                throw new \Exception(sprintf("Can't open '%s' editor.", $editor));
113
            }
114
        }
115
    }
116
}
117