<commit_before><?php

namespace Shopware\Install\Services;


use ShopwareCli\Config;
use ShopwareCli\Services\ProcessExecutor;

class PostInstall
{
    /**
     * @var \ShopwareCli\Services\ProcessExecutor
     */
    private $processExecutor;
    /**
     * @var Owner
     */
    private $owner;
    /**
     * @var \ShopwareCli\Config
     */
    private $config;

    public function __construct(ProcessExecutor $processExecutor, Owner $owner, Config $config)
    {
        $this->processExecutor = $processExecutor;
        $this->owner = $owner;
        $this->config = $config;
    }

    /**
     * Set permissions for the shopware directory
     *
     * @param $directory
     */
    public function fixPermissions($directory)
    {
        $command = sprintf('chmod 0777 -R "%s"', $directory . '/logs');
        $this->processExecutor->execute($command);

        $command = sprintf('chmod 0777 -R "%s"', $directory . '/cache');
        $this->processExecutor->execute($command);

        $this->setUser($directory);
        $this->setGroup($directory);
    }

    /**
     * Import custom deltas
     *
     * @param $database
     * @throws \RuntimeException
     */
    public function importCustomDeltas($database)
    {
        if (!isset($this->config['CustomDeltas'])) {
            return;
        }

        $connection = $this->getConnection();
        $connection->query("USE `{$database}`");

        foreach ($this->config['CustomDeltas'] as $file) {
            if (!file_exists($file)) {
                throw new \RuntimeException("File '{$file}' not found");
            }
            $connection->exec(file_get_contents($file));
        }
    }

    /**
     * Run user scripts
     *
     * @param $path
     */
    public function runCustomScripts($path)
    {
        if (!isset($this->config['CustomScripts'])) {
            return;
        }

        foreach ($this->config['CustomScripts'] as $script) {
            $this->processExecutor->execute($script, $path, true);
        }
    }

    /**
     * set the user for the shopware directory
     *
     * @param $directory
     */
    private function setUser($directory)
    {
        if (isset($this->config['ChangeOwner']['user'])) {
            $this->owner->setUser($directory, $this->config['ChangeOwner']['user'], true);
        }
    }

    /**
     * set the group for the shopware directory
     *
     * @param $directory
     */
    private function setGroup($directory)
    {
        if (isset($this->config['ChangeOwner']['group'])) {
            $this->owner->setGroup($directory, $this->config['ChangeOwner']['group'], true);
        }
    }

    /**
     * Get a PDO connection
     *
     * @return \PDO
     */
    private function getConnection()
    {
        $username = $this->config['DatabaseConfig']['user'];
        $password = $this->config['DatabaseConfig']['pass'];
        $host = $this->config['DatabaseConfig']['host'];

        $connection = new \PDO("mysql:host={$host};charset=utf8", $username, $password);
        $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

        return $connection;
    }
}<commit_msg>Fixes notice if ChangeOwner config is not set<commit_after><?php

namespace Shopware\Install\Services;

use ShopwareCli\Config;
use ShopwareCli\Services\ProcessExecutor;

class PostInstall
{
    /**
     * @var \ShopwareCli\Services\ProcessExecutor
     */
    private $processExecutor;
    /**
     * @var Owner
     */
    private $owner;
    /**
     * @var \ShopwareCli\Config
     */
    private $config;

    public function __construct(ProcessExecutor $processExecutor, Owner $owner, Config $config)
    {
        $this->processExecutor = $processExecutor;
        $this->owner = $owner;
        $this->config = $config;
    }

    /**
     * Set permissions for the shopware directory
     *
     * @param $directory
     */
    public function fixPermissions($directory)
    {
        $command = sprintf('chmod 0777 -R "%s"', $directory . '/logs');
        $this->processExecutor->execute($command);

        $command = sprintf('chmod 0777 -R "%s"', $directory . '/cache');
        $this->processExecutor->execute($command);

        $this->setUser($directory);
        $this->setGroup($directory);
    }

    /**
     * Import custom deltas
     *
     * @param $database
     * @throws \RuntimeException
     */
    public function importCustomDeltas($database)
    {
        if (!isset($this->config['CustomDeltas'])) {
            return;
        }

        $connection = $this->getConnection();
        $connection->query("USE `{$database}`");

        foreach ($this->config['CustomDeltas'] as $file) {
            if (!file_exists($file)) {
                throw new \RuntimeException("File '{$file}' not found");
            }
            $connection->exec(file_get_contents($file));
        }
    }

    /**
     * Run user scripts
     *
     * @param $path
     */
    public function runCustomScripts($path)
    {
        if (!isset($this->config['CustomScripts'])) {
            return;
        }

        foreach ($this->config['CustomScripts'] as $script) {
            $this->processExecutor->execute($script, $path, true);
        }
    }

    /**
     * set the user for the shopware directory
     *
     * @param $directory
     */
    private function setUser($directory)
    {
        if (isset($this->config['ChangeOwner'], $this->config['ChangeOwner']['user'])) {
            $this->owner->setUser($directory, $this->config['ChangeOwner']['user'], true);
        }
    }

    /**
     * set the group for the shopware directory
     *
     * @param $directory
     */
    private function setGroup($directory)
    {
        if (isset($this->config['ChangeOwner'], $this->config['ChangeOwner']['group'])) {
            $this->owner->setGroup($directory, $this->config['ChangeOwner']['group'], true);
        }
    }

    /**
     * Get a PDO connection
     *
     * @return \PDO
     */
    private function getConnection()
    {
        $username = $this->config['DatabaseConfig']['user'];
        $password = $this->config['DatabaseConfig']['pass'];
        $host = $this->config['DatabaseConfig']['host'];

        $connection = new \PDO("mysql:host={$host};charset=utf8", $username, $password);
        $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

        return $connection;
    }
}
{'fraction_non_alphanumeric': 0.1589383294301327, 'fraction_numerical': 0.002810304449648712, 'mean_word_length': 2.283444387493593, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2978010', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1794, 'n_words': 349}