Completed
Push — master ( 36e8c5...2454a8 )
by Tom
12:20 queued 07:25
created

UsageCommand::findEmailTemplates()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 41
rs 8.439
cc 5
eloc 21
nc 9
nop 0
1
<?php
2
/**
3
 * this file is part of magerun
4
 *
5
 * @author Mike Parkin <https://github.com/MikeParkin>
6
 * @author Tom Klingenberg <https://github.com/ktomk>
7
 */
8
namespace N98\Magento\Command\Developer\EmailTemplate;
9
10
use Mage;
11
use Mage_Adminhtml_Model_Email_Template;
12
use Mage_Core_Model_Template;
13
use N98\Magento\Command\AbstractMagentoCommand;
14
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class UsageCommand extends AbstractMagentoCommand
20
{
21 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        $this
24
            ->setName('dev:email-template:usage')
25
            ->setDescription('Display database transactional email template usage')
26
            ->addOption(
27
                'format',
28
                null,
29
                InputOption::VALUE_OPTIONAL,
30
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
31
            );
32
    }
33
34
    /**
35
     * @param InputInterface $input
36
     * @param OutputInterface $output
37
     *
38
     * @return int|void
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $this->detectMagento($output, true);
43
        $this->initMagento();
44
        $templates = $this->findEmailTemplates();
45
46
        if (!empty($templates)) {
47
            $this->getHelper('table')
48
                ->setHeaders(array('id', 'Name', 'Scope', 'Scope Id', 'Path'))
49
                ->renderByFormat($output, $templates, $input->getOption('format'));
50
        } else {
51
            $output->writeln("No transactional email templates stored in the database.");
52
        }
53
    }
54
55
    protected function findEmailTemplates()
56
    {
57
        /** @var Mage_Core_Model_Template[] $templates */
58
        $templates = Mage::getModel('adminhtml/email_template')->getCollection();
59
60
        $return = array();
61
62
        foreach ($templates as $template) {
63
64
            /**
65
             * Some modules overload the template class so that the method getSystemConfigPathsWhereUsedCurrently
66
             * is not available, this is a workaround for that
67
             */
68
            if (!method_exists($template, 'getSystemConfigPathsWhereUsedCurrently')) {
69
                $instance = new Mage_Adminhtml_Model_Email_Template();
70
                $template = $instance->load($template->getId());
71
            }
72
73
            $configPaths = $template->getSystemConfigPathsWhereUsedCurrently();
74
75
            if (!count($configPaths)) {
76
                $configPaths[] = array(
77
                    'scope'    => 'Unused',
78
                    'scope_id' => 'Unused',
79
                    'path'     => 'Unused',
80
                );
81
            }
82
83
            foreach ($configPaths as $configPath) {
84
                $return[] = array(
85
                    'id'            => $this->sanitizeEmailProperty($template->getId()),
86
                    'Template Code' => $this->sanitizeEmailProperty($template->getTemplateCode()),
87
                    'Scope'         => $this->sanitizeEmailProperty($configPath['scope']),
88
                    'Scope Id'      => $this->sanitizeEmailProperty($configPath['scope_id']),
89
                    'Path'          => $this->sanitizeEmailProperty($configPath['path']),
90
                );
91
            }
92
        }
93
94
        return $return;
95
    }
96
97
    /**
98
     * @param string $input Module property to be sanitized
99
     *
100
     * @return string
101
     */
102
    private function sanitizeEmailProperty($input)
103
    {
104
        return trim($input);
105
    }
106
}
107