Completed
Pull Request — 4.0 (#4075)
by Kentaro
08:21 queued 02:09
created

LoadDataFixturesEccubeCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 6.48 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 0
Metric Value
dl 7
loc 108
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 11 1
B execute() 7 80 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Command;
15
16
use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand;
17
use Eccube\Common\EccubeConfig;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
23
class LoadDataFixturesEccubeCommand extends DoctrineCommand
24
{
25
    protected static $defaultName = 'eccube:fixtures:load';
26
27
    /**
28
     * @var ContainerInterface
29
     */
30
    protected $container;
31
32
    public function __construct(ContainerInterface $container)
33
    {
34
        parent::__construct();
35
        $this->container = $container;
36
    }
37
38
    protected function configure()
39
    {
40
        $this
41
            ->setDescription('Load data fixtures to your database.')
42
            ->setHelp(<<<EOF
43
The <info>%command.name%</info> command loads data fixtures from EC-CUBE.
44
45
  <info>php %command.full_name%</info>
46
EOF
47
            );
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $em = $this->getEntityManager(null);
53
54
        // for full locale code cases
55
        $locale = env('ECCUBE_LOCALE', 'ja_JP');
56
        $locale = str_replace('_', '-', $locale);
57
        $locales = \Locale::parseLocale($locale);
58
        $localeDir = is_null($locales) ? 'ja' : $locales['language'];
59
60
        $loader = new \Eccube\Doctrine\Common\CsvDataFixtures\Loader();
61
        $loader->loadFromDirectory(__DIR__.'/../Resource/doctrine/import_csv/'.$localeDir);
62
        $executer = new \Eccube\Doctrine\Common\CsvDataFixtures\Executor\DbalExecutor($em);
63
        $fixtures = $loader->getFixtures();
64
        $executer->execute($fixtures);
65
66
        $login_id = env('ECCUBE_ADMIN_USER', 'admin');
67
        $login_password = env('ECCUBE_ADMIN_PASS', 'password');
68
69
        $eccubeConfig = $this->container->get(EccubeConfig::class);
70
        $encoder = new \Eccube\Security\Core\Encoder\PasswordEncoder($eccubeConfig);
71
72
        $salt = \Eccube\Util\StringUtil::random(32);
73
        $password = $encoder->encodePassword($login_password, $salt);
74
75
        $conn = $em->getConnection();
76
        $member_id = ('postgresql' === $conn->getDatabasePlatform()->getName())
77
            ? $conn->fetchColumn("select nextval('dtb_member_id_seq')")
78
            : null;
79
80
        $conn->insert('dtb_member', [
81
            'id' => $member_id,
82
            'login_id' => $login_id,
83
            'password' => $password,
84
            'salt' => $salt,
85
            'work_id' => 1,
86
            'authority_id' => 0,
87
            'creator_id' => 1,
88
            'sort_no' => 1,
89
            'update_date' => new \DateTime(),
90
            'create_date' => new \DateTime(),
91
            'name' => trans('install.member_name'),
92
            'department' => 'EC-CUBE SHOP',
93
            'discriminator_type' => 'member',
94
        ], [
95
            'update_date' => \Doctrine\DBAL\Types\Type::DATETIME,
96
            'create_date' => \Doctrine\DBAL\Types\Type::DATETIME,
97
        ]);
98
99
        $shop_name = env('ECCUBE_SHOP_NAME', 'EC-CUBE SHOP');
100
        $admin_mail = env('ECCUBE_ADMIN_MAIL', '[email protected]');
101
102
        $id = ('postgresql' === $conn->getDatabasePlatform()->getName())
103
            ? $conn->fetchColumn("select nextval('dtb_base_info_id_seq')")
104
            : null;
105
106
        $conn->insert('dtb_base_info', [
107
            'id' => $id,
108
            'shop_name' => $shop_name,
109
            'email01' => $admin_mail,
110
            'email02' => $admin_mail,
111
            'email03' => $admin_mail,
112
            'email04' => $admin_mail,
113
            'update_date' => new \DateTime(),
114
            'discriminator_type' => 'baseinfo',
115
        ], [
116
            'update_date' => \Doctrine\DBAL\Types\Type::DATETIME,
117
        ]);
118
119
        $faviconPath = '/assets/img/common/favicon.ico';
120 View Code Duplication
        if (!file_exists($this->container->getParameter('eccube_html_dir').'/user_data'.$faviconPath)) {
121
            $file = new Filesystem();
122
            $file->copy(
123
                $this->container->getParameter('eccube_html_front_dir').$faviconPath,
124
                $this->container->getParameter('eccube_html_dir').'/user_data'.$faviconPath
125
            );
126
        }
127
128
        $output->writeln(sprintf('  <comment>></comment> <info>%s</info>', 'Finished Successful!'));
129
    }
130
}
131