<commit_before><?php
namespace Mapbender\SearchBundle\Controller;

use FOM\UserBundle\Entity\User;
use Mapbender\CoreBundle\Component\SecurityContext;
use Mapbender\SearchBundle\Component\StyleManager;
use Mapbender\SearchBundle\Utils\HTTPStatusConstants;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class StyleController
 *
 * @package Mapbender\SearchBundle\Element
 * @author  <NAME> <<EMAIL>>
 * @Route("/style/")
 */
class StyleController extends MapbenderController
{

    /** @var SecurityContext */
    protected $securityContext;

    /** @var StyleManager */
    protected $styleManager;

    /** @var User */
    protected $user;

    /**
     *This method has desired services injected.
     *
     * @return string[]
     */
    protected function mappings()
    {
        return array("styleManager"    => static::MAPBENDER_STYLE_MANAGER,
                     "securityContext" => static::MAPBENDER_SECURITY_CONTEXT
        );
    }


    /**
     * @param       $message
     * @param int   $status
     * @param array $headers
     * @return JsonResponse
     */
    private function getErrorMessage($message, $status = HTTPStatusConstants::_BAD_REQUEST, $headers = array())
    {
        $errors = array('errors' => array('message' => $message));
        return new JsonResponse($errors, $status, $headers);
    }

    /**
     * @param int   $id
     * @param int   $status
     * @param array $headers
     * @return JsonResponse
     */
    private function getEmptyMessage($id, $status = HTTPStatusConstants::_NOT_FOUND, $headers = array())
    {
        $errors = array('errors' => array('message' => "Could not alter/find stylemap for id " . $id));
        return new JsonResponse($errors, $status, $headers);
    }


    /**
     * @Route("{id}",requirements={"id" = "\w+"})
     * @param int $int
     * @Method("GET")
     * @return JsonResponse
     */
    public function get($id)
    {
        if ($this->securityContext->isUserAllowedToView($this->user)) {
            $styleMap = $this->styleManager->getById($id);

            if ($styleMap != null) {
                return $this->getSuccessMessage($styleMap);
            } else {
                return $this->getEmptyMessage($id);
            }
        }

        return $this->getErrorMessage("Get: Current user is not authorized to access style with id " . $id, HTTPStatusConstants::_UNAUTHORIZED);

    }

    /**
     * Updates or creates new StyleMap
     * @Route("update")
     * @Method("POST")
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function update(Request $request)
    {

        if ($this->securityContext->isUserAllowedToEdit($this->user)) {
            $styles = $request->request->all();

            $styleMap = $this->styleManager->update($styles);

            return $this->getSuccessMessage($styleMap);
        }
        return $this->getErrorMessage("Update : Current user is not authorized to access style with id " . $request->get("id"), HTTPStatusConstants::_UNAUTHORIZED);

    }


    /**
     * @Route("{id}/remove",requirements={"id" = "\w+"})
     * @Method("GET")
     * @return JsonResponse
     */
    public function remove($id)
    {
        if ($this->securityContext->isUserAllowedToDelete($this->user)) {
            $isRemoved = $this->styleManager->remove($id);
            return $isRemoved ? $this->getSuccessMessage($isRemoved) : $this->getEmptyMessage($id);
        }
        return $this->getErrorMessage("Remove: Current user is not authorized to access style with id " . $id, HTTPStatusConstants::_UNAUTHORIZED);
    }

    /**
     * @param mixed $payload
     * @param int   $status
     * @param array $headers
     * @return JsonResponse
     */
    private function getSuccessMessage($payload, $status = HTTPStatusConstants::_OK, $headers = array())
    {
        return new JsonResponse($payload, $status, $headers);
    }


}<commit_msg>Add HKV conform controller logic<commit_after><?php
namespace Mapbender\SearchBundle\Controller;

use Eslider\Driver\HKVStorage;
use Eslider\Entity\HKV;
use FOM\UserBundle\Entity\User;
use Mapbender\CoreBundle\Component\SecurityContext;
use Mapbender\SearchBundle\Component\StyleManager;
use Mapbender\SearchBundle\Entity\StyleMap;
use Mapbender\SearchBundle\Utils\HTTPStatusConstants;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class StyleController
 *
 * @package Mapbender\SearchBundle\Element
 * @author  <NAME> <<EMAIL>>
 * @Route("/style/")
 */
class StyleController extends MapbenderController
{

    /** @var SecurityContext */
    protected $securityContext;

    /** @var StyleManager */
    protected $styleManager;

    /** @var User */
    protected $user;

    /**
     *This method has desired services injected.
     *
     * @return string[]
     */
    protected function mappings()
    {
        return array("styleManager"    => static::MAPBENDER_STYLE_MANAGER,
                     "securityContext" => static::MAPBENDER_SECURITY_CONTEXT
        );
    }


    /**
     * @param       $message
     * @param int   $status
     * @param array $headers
     * @return Response
     */
    private function getErrorMessage($message, $status = HTTPStatusConstants::_BAD_REQUEST, $headers = array())
    {
        return new Response($message, $status, $headers);
    }

    /**
     * @param int   $id
     * @param int   $status
     * @param array $headers
     * @return Response
     */
    private function getEmptyMessage($id, $status = HTTPStatusConstants::_NOT_FOUND, $headers = array())
    {

        return new Response("Could not alter/find stylemap for id " . $id, $status, $headers);
    }


    /**
     * @Route("get/{id}")
     * @param int $int
     * @Method("GET")
     * @return Response
     */
    public function get($id)
    {
        if ($this->securityContext->isUserAllowedToView($this->user)) {
            $styleMap = $this->styleManager->getById($id);
            if ($styleMap != null) {
                return $this->getSuccessMessage($styleMap);
            } else {
                return $this->getEmptyMessage($id);
            }
        }
        return $this->getErrorMessage("Get: Current user is not authorized to access style with id " . $id, HTTPStatusConstants::_UNAUTHORIZED);

    }

    /**
     * Updates or creates new StyleMap
     * @Route("save")
     * @Method("POST")
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request)
    {

        if ($this->securityContext->isUserAllowedToEdit($this->user)) {
            $styles = $request->request->all();

            $styleMap = $this->styleManager->saveArray($styles);
            return $this->getSuccessMessage($styleMap);
        }
        return $this->getErrorMessage("Update : Current user is not authorized to access style with id " . $request->get("id"), HTTPStatusConstants::_UNAUTHORIZED);

    }


    /**
     * @param StyleMap|HKV $styleMap
     * @param int          $status
     * @param array        $headers
     * @return Response
     */
    private function getSuccessMessage($styleMap, $status = HTTPStatusConstants::_OK, $headers = array())
    {
        return new Response(HKVStorage::encodeValue($styleMap), $status, $headers);

    }


}
{'fraction_non_alphanumeric': 0.13282782063014772, 'fraction_numerical': 0.0, 'mean_word_length': 2.729887859580692, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 61, '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': '1529760', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2148, 'n_words': 465}