<commit_before><?php

namespace Lighthouse\Http\Controllers\Api\V1;

use Log;
use Lighthouse\Services\Torrents\Entities\Error;
use Illuminate\Http\Request;
use Lighthouse\Http\Controllers\Controller;
use Lighthouse\Services\Torrents\Common\OperationResult;
use Lighthouse\Services\Torrents\Common\ResultCodes;
use Lighthouse\Services\Torrents\Contracts\Service;
use Lighthouse\Services\Torrents\Entities\ServiceQuery;
use Lighthouse\Services\Torrents\Entities\Torrent;
use Symfony\Component\HttpFoundation\Response;

class Torrents extends Controller
{
    protected $service;

    public function __construct(Service $service)
    {
        $this->service = $service;
    }

    /**
     * @param Request $request
     *
     * @return Response
     */
    public function search(Request $request)
    {
        $query = $this->buildQuery($request->input());
        $operationResult = $this->service->search($query);

        return $this->prepareResponse($operationResult);
    }

    /**
     * @param string $torrentHash
     *
     * @return Response
     */
    public function get($torrentHash)
    {
        $operationResult = $this->service->get($torrentHash);

        return $this->prepareResponse($operationResult);
    }

    private function prepareResponse(OperationResult $result)
    {
        $code = $this->mapToHttpCode($result->getCode());
        $body = $result->isSuccessful()
            ? $this->prepareTorrents($result->getData())
            : $this->prepareErrorMessage($result->getError());

        return response($body, $code);
    }

    /**
     * @param array|Torrent $data
     *
     * @return array|Torrent
     */
    private function prepareTorrents($data)
    {
        if (is_array($data)) {
            return array_map([$this, 'prepareTorrent'], $data);
        }

        return $this->prepareTorrent($data);
    }

    /**
     * @param Torrent $torrent
     *
     * @return array
     */
    private function prepareTorrent($torrent)
    {
        return $torrent->toArray();
    }

    /**
     * @param int $resultCode
     *
     * @return int
     */
    private function mapToHttpCode($resultCode)
    {
        switch ($resultCode) {
            case ResultCodes::Successful:
                return 200;
            case ResultCodes::ResourceCreated;
                return 201;
            case ResultCodes::InvalidInput:
                return 400;
            case ResultCodes::ResourceNotFound:
                return 404;
            case ResultCodes::ServiceError:
                return 500;
            default:
                throw new \InvalidArgumentException('Service returned unsupported result code.');
        }
    }

    /**
     * @param array $input
     *
     * @return ServiceQuery
     */
    private function buildQuery($input)
    {
        $params = [
            'phrase' => array_key_exists('q', $input) ? $input['q'] : '',
        ];

        if (array_key_exists('size', $input)) {
            $params['size'] = intval($input['size']);
        }

        if (array_key_exists('category', $input)) {
            $params['category'] = strtolower($input['category']);
        }

        return new ServiceQuery($params);
    }

    /**
     * @param $error
     * @return array
     */
    private function prepareErrorMessage(Error $error)
    {
        return ['error' => $error->toArray()];
    }
}<commit_msg>Applied fixes from StyleCI<commit_after><?php

namespace Lighthouse\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Lighthouse\Http\Controllers\Controller;
use Lighthouse\Services\Torrents\Common\OperationResult;
use Lighthouse\Services\Torrents\Common\ResultCodes;
use Lighthouse\Services\Torrents\Contracts\Service;
use Lighthouse\Services\Torrents\Entities\Error;
use Lighthouse\Services\Torrents\Entities\ServiceQuery;
use Lighthouse\Services\Torrents\Entities\Torrent;
use Symfony\Component\HttpFoundation\Response;

class Torrents extends Controller
{
    protected $service;

    public function __construct(Service $service)
    {
        $this->service = $service;
    }

    /**
     * @param Request $request
     *
     * @return Response
     */
    public function search(Request $request)
    {
        $query = $this->buildQuery($request->input());
        $operationResult = $this->service->search($query);

        return $this->prepareResponse($operationResult);
    }

    /**
     * @param string $torrentHash
     *
     * @return Response
     */
    public function get($torrentHash)
    {
        $operationResult = $this->service->get($torrentHash);

        return $this->prepareResponse($operationResult);
    }

    private function prepareResponse(OperationResult $result)
    {
        $code = $this->mapToHttpCode($result->getCode());
        $body = $result->isSuccessful()
            ? $this->prepareTorrents($result->getData())
            : $this->prepareErrorMessage($result->getError());

        return response($body, $code);
    }

    /**
     * @param array|Torrent $data
     *
     * @return array|Torrent
     */
    private function prepareTorrents($data)
    {
        if (is_array($data)) {
            return array_map([$this, 'prepareTorrent'], $data);
        }

        return $this->prepareTorrent($data);
    }

    /**
     * @param Torrent $torrent
     *
     * @return array
     */
    private function prepareTorrent($torrent)
    {
        return $torrent->toArray();
    }

    /**
     * @param int $resultCode
     *
     * @return int
     */
    private function mapToHttpCode($resultCode)
    {
        switch ($resultCode) {
            case ResultCodes::Successful:
                return 200;
            case ResultCodes::ResourceCreated;

                return 201;
            case ResultCodes::InvalidInput:
                return 400;
            case ResultCodes::ResourceNotFound:
                return 404;
            case ResultCodes::ServiceError:
                return 500;
            default:
                throw new \InvalidArgumentException('Service returned unsupported result code.');
        }
    }

    /**
     * @param array $input
     *
     * @return ServiceQuery
     */
    private function buildQuery($input)
    {
        $params = [
            'phrase' => array_key_exists('q', $input) ? $input['q'] : '',
        ];

        if (array_key_exists('size', $input)) {
            $params['size'] = intval($input['size']);
        }

        if (array_key_exists('category', $input)) {
            $params['category'] = strtolower($input['category']);
        }

        return new ServiceQuery($params);
    }

    /**
     * @param $error
     *
     * @return array
     */
    private function prepareErrorMessage(Error $error)
    {
        return ['error' => $error->toArray()];
    }
}
{'fraction_non_alphanumeric': 0.12812224507787245, 'fraction_numerical': 0.0047017337643255955, 'mean_word_length': 2.2460658082975677, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '6632999', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1886, 'n_words': 353}