<commit_before><?php

namespace Oro\Bundle\TranslationBundle\Provider;

class CrowdinAdapter extends AbstractAPIAdapter
{
    /** Crowdin folder exists */
    const DIR_ALREADY_EXISTS = 13;

    /**
     * @var string
     */
    protected $projectId;

    /**
     * Add or update file API method
     *
     * @param string $remotePath Path in remove API service
     * @param string $file
     * @param string $mode       'add' or 'update'
     *
     * @return mixed array with xml strings
     */
    public function addFile($remotePath, $file, $mode = 'add')
    {
        $result = $this->request(
            sprintf('/project/%s/%s-file', $this->projectId, $mode),
            array(
                sprintf('files[%s]', $remotePath) => '@' . $file,
            ),
            'POST'
        );

        return $result;
    }

    /**
     * @param string $dir
     *
     * @return bool
     */
    public function addDirectory($dir)
    {
        $this->request(
            '/project/' . $this->projectId . '/add-directory',
            array(
                'name' => $dir,
            ),
            'POST'
        );

        return true;
    }

    /**
     * @param array $dirs
     *
     * @throws \Exception
     * @return $this
     */
    public function createDirectories($dirs)
    {
        $i = 0;
        foreach ($dirs as $dir) {
            try {
                $i++;
                $this->addDirectory($dir);

                $this->notifyProgress(
                    sprintf('%0.2f%%', $i * 100 / count($dirs)) .
                    sprintf(' Directory <info>%s</info> created', $dir)
                );
            } catch (\Exception $e) {
                if ($e->getCode() !== self::DIR_ALREADY_EXISTS) {
                    throw $e;
                }

                $this->notifyProgress(
                    sprintf('%0.2f%%', $i * 100 / count($dirs)) .
                    sprintf(' Directory <info>%s</info> already exists, skipping...', $dir)
                );
            }
        }

        return $this;
    }

    /**
     * @param string $files
     * @param string $mode 'add' or 'update'
     *
     * @return array
     */
    public function uploadFiles($files, $mode)
    {
        $results = array();
        $failed  = array();
        $i       = 0;

        foreach ($files as $apiPath => $filePath) {
            try {
                $results[] = $this->addFile($apiPath, $filePath, $mode);
                $message   = sprintf('File <info>%s</info> uploaded', $apiPath);
            } catch (\Exception $e) {
                $failed[$filePath] = $e->getMessage();
                $message           = sprintf(
                    'File <info>%s</info> upload failed: <error>%s</error>',
                    $apiPath,
                    $e->getMessage()
                );
            }

            $i++;
            $this->notifyProgress(
                sprintf('%0.2f%%', $i * 100 / count($files)),
                $message
            );
        }

        return array('results' => $results, 'failed' => $failed);
    }

    /**
     * {@inheritdoc}
     */
    protected function request($uri, $data = array(), $method = 'GET')
    {
        $result = parent::request($uri, $data, $method);
        $result = new \SimpleXMLElement($result);

        if ($result->getName() == 'error') {
            $message = $result->message;
            throw new \Exception($message, (int)$result->code);
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function upload($files, $mode = 'add')
    {
        if (empty($files)) {
            return false;
        }

        // compile dir list
        $dirs = array();
        foreach ($files as $apiPath => $filePath) {
            $_dirs = explode(DIRECTORY_SEPARATOR, dirname($apiPath));

            $currentDir = array();
            foreach ($_dirs as $dir) {
                $currentDir[] = $dir;
                $path         = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $currentDir);
                $dirs[$path]  = $path;
            }
        }

        return $this->createDirectories($dirs)
            ->uploadFiles($files, $mode);
    }

    /**
     * @param string $projectId
     */
    public function setProjectId($projectId)
    {
        $this->projectId = $projectId;
    }
}
<commit_msg>BAP-1804: Translation API - fix crowdin upload<commit_after><?php

namespace Oro\Bundle\TranslationBundle\Provider;

class CrowdinAdapter extends AbstractAPIAdapter
{
    /** Crowdin folder exists */
    const DIR_ALREADY_EXISTS = 13;

    /**
     * @var string
     */
    protected $projectId;

    /**
     * Add or update file API method
     *
     * @param string $remotePath Path in remove API service
     * @param string $file
     * @param string $mode       'add' or 'update'
     *
     * @return mixed array with xml strings
     */
    public function addFile($remotePath, $file, $mode = 'add')
    {
        $result = $this->request(
            sprintf('/project/%s/%s-file', $this->projectId, $mode),
            array(
                sprintf('files[%s]', $remotePath) => '@' . $file,
                sprintf('export_patterns[%s]', $remotePath) => preg_replace(
                    '#\.[\w_]{2,5}\.(\w+)$#',
                    '.%locale_with_underscore%.$1',
                    $remotePath
                ),
            ),
            'POST'
        );

        return $result;
    }

    /**
     * @param string $dir
     *
     * @return bool
     */
    public function addDirectory($dir)
    {
        $this->request(
            '/project/' . $this->projectId . '/add-directory',
            array(
                'name' => $dir,
            ),
            'POST'
        );

        return true;
    }

    /**
     * @param array $dirs
     *
     * @throws \Exception
     * @return $this
     */
    public function createDirectories($dirs)
    {
        $i = 0;
        foreach ($dirs as $dir) {
            try {
                $i++;
                $this->addDirectory($dir);

                $this->notifyProgress(
                    sprintf('%0.2f%%', $i * 100 / count($dirs)) .
                    sprintf(' Directory <info>%s</info> created', $dir)
                );
            } catch (\Exception $e) {
                if ($e->getCode() !== self::DIR_ALREADY_EXISTS) {
                    throw $e;
                }

                $this->notifyProgress(
                    sprintf('%0.2f%%', $i * 100 / count($dirs)) .
                    sprintf(' Directory <info>%s</info> already exists, skipping...', $dir)
                );
            }
        }

        return $this;
    }

    /**
     * @param string $files
     * @param string $mode 'add' or 'update'
     *
     * @return array
     */
    public function uploadFiles($files, $mode)
    {
        $results = array();
        $failed  = array();
        $i       = 0;

        foreach ($files as $apiPath => $filePath) {
            try {
                $results[] = $this->addFile($apiPath, $filePath, $mode);
                $message   = sprintf('File <info>%s</info> uploaded', $apiPath);
            } catch (\Exception $e) {
                $failed[$filePath] = $e->getMessage();
                $message           = sprintf(
                    'File <info>%s</info> upload failed: <error>%s</error>',
                    $apiPath,
                    $e->getMessage()
                );
            }

            $i++;
            $this->notifyProgress(
                sprintf('%0.2f%%', $i * 100 / count($files)),
                $message
            );
        }

        return array('results' => $results, 'failed' => $failed);
    }

    /**
     * {@inheritdoc}
     */
    protected function request($uri, $data = array(), $method = 'GET')
    {
        $result = parent::request($uri, $data, $method);
        $result = new \SimpleXMLElement($result);

        if ($result->getName() == 'error') {
            $message = $result->message;
            throw new \Exception($message, (int)$result->code);
        }

        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function upload($files, $mode = 'add')
    {
        if (empty($files)) {
            return false;
        }

        // compile dir list
        $dirs = array();
        foreach ($files as $apiPath => $filePath) {
            $_dirs = explode(DIRECTORY_SEPARATOR, dirname($apiPath));

            $currentDir = array();
            foreach ($_dirs as $dir) {
                $currentDir[] = $dir;
                $path         = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $currentDir);
                $dirs[$path]  = $path;
            }
        }

        return $this->createDirectories($dirs)
            ->uploadFiles($files, $mode);
    }

    /**
     * @param string $projectId
     */
    public function setProjectId($projectId)
    {
        $this->projectId = $projectId;
    }
}

{'fraction_non_alphanumeric': 0.150561547870566, 'fraction_numerical': 0.005003891915934616, 'mean_word_length': 1.3901142705288334, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7148900', 'n_tokens_mistral': 2660, 'n_tokens_neox': 2546, 'n_words': 551}