<commit_before><?php

/**
 * Copyright 2014 SURFnet bv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Repository;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping;
use Surfnet\Stepup\Identity\Value\IdentityId;
use Surfnet\Stepup\Identity\Value\Institution;
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery;

class RaListingRepository extends EntityRepository
{
    /**
     * @var InstitutionAuthorizationRepositoryFilter
     */
    private $authorizationRepositoryFilter;

    public function __construct(
        EntityManager $em,
        Mapping\ClassMetadata $class,
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
    ) {
        parent::__construct($em, $class);
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
    }

    /**
     * @param IdentityId $identityId The RA's identity id.
     * @return null|RaListing
     */
    public function findByIdentityId(IdentityId $identityId)
    {
        return parent::findOneBy(['identityId' => (string) $identityId]);
    }

    /**
     * @param IdentityId $identityId The RA's identity id.
     * @param Institution $raInstitution
     * @return null|RaListing
     */
    public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
    {
        return parent::findOneBy([
            'identityId' => (string) $identityId,
            'raInstitution' => (string) $raInstitution,
        ]);
    }

    /**
     * @param IdentityId $identityId The RA's identity id.
     * @param Institution $institution
     * @return RaListing[]
     */
    public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
    {
        return parent::findBy([
            'identityId' => (string) $identityId,
            'institution' => (string) $institution,
        ]);
    }

    public function save(RaListing $raListingEntry)
    {
        $this->getEntityManager()->persist($raListingEntry);
        $this->getEntityManager()->flush();
    }

    /**
     * @param RaListingQuery $query
     * @return \Doctrine\ORM\Query
     */
    public function createSearchQuery(RaListingQuery $query)
    {
        $queryBuilder = $this->createQueryBuilder('r');

        // Modify query to filter on authorization
        $this->authorizationRepositoryFilter->filter(
            $queryBuilder,
            $query->authorizationContext,
            ['r.identityId', 'r.institution'],
            'r.institution',
            'iac'
        );

        if ($query->institution) {
            $queryBuilder
                ->andWhere('r.institution = :institution')
                ->setParameter('institution', $query->institution);
        }

        if ($query->identityId) {
            $queryBuilder
                ->andWhere('r.identityId = :identityId')
                ->setParameter('identityId', (string) $query->identityId);
        }

        if (!$query->orderBy) {
            return $queryBuilder->getQuery();
        }

        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';

        switch ($query->orderBy) {
            case 'commonName':
                $queryBuilder->orderBy('r.commonName', $orderDirection);
                break;
            default:
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
        }<commit_msg>Group the ra listing query by ra institution<commit_after><?php

/**
 * Copyright 2014 SURFnet bv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Repository;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping;
use Surfnet\Stepup\Identity\Value\IdentityId;
use Surfnet\Stepup\Identity\Value\Institution;
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery;

class RaListingRepository extends EntityRepository
{
    /**
     * @var InstitutionAuthorizationRepositoryFilter
     */
    private $authorizationRepositoryFilter;

    public function __construct(
        EntityManager $em,
        Mapping\ClassMetadata $class,
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
    ) {
        parent::__construct($em, $class);
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
    }

    /**
     * @param IdentityId $identityId The RA's identity id.
     * @return null|RaListing
     */
    public function findByIdentityId(IdentityId $identityId)
    {
        return parent::findOneBy(['identityId' => (string) $identityId]);
    }

    /**
     * @param IdentityId $identityId The RA's identity id.
     * @param Institution $raInstitution
     * @return null|RaListing
     */
    public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
    {
        return parent::findOneBy([
            'identityId' => (string) $identityId,
            'raInstitution' => (string) $raInstitution,
        ]);
    }

    /**
     * @param IdentityId $identityId The RA's identity id.
     * @param Institution $institution
     * @return RaListing[]
     */
    public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
    {
        return parent::findBy([
            'identityId' => (string) $identityId,
            'institution' => (string) $institution,
        ]);
    }

    public function save(RaListing $raListingEntry)
    {
        $this->getEntityManager()->persist($raListingEntry);
        $this->getEntityManager()->flush();
    }

    /**
     * @param RaListingQuery $query
     * @return \Doctrine\ORM\Query
     */
    public function createSearchQuery(RaListingQuery $query)
    {
        $queryBuilder = $this->createQueryBuilder('r');

        // Modify query to filter on authorization
        $this->authorizationRepositoryFilter->filter(
            $queryBuilder,
            $query->authorizationContext,
            ['r.identityId', 'r.institution', 'r.raInstitution'],
            'r.institution',
            'iac'
        );

        if ($query->institution) {
            $queryBuilder
                ->andWhere('r.institution = :institution')
                ->setParameter('institution', $query->institution);
        }

        if ($query->identityId) {
            $queryBuilder
                ->andWhere('r.identityId = :identityId')
                ->setParameter('identityId', (string) $query->identityId);
        }

        if (!$query->orderBy) {
            return $queryBuilder->getQuery();
        }

        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';

        switch ($query->orderBy) {
            case 'commonName':
                $queryBuilder->orderBy('r.commonName', $orderDirection);
                break;
            default:
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
        }
{'fraction_non_alphanumeric': 0.10831988937543213, 'fraction_numerical': 0.0018437427978796957, 'mean_word_length': 2.836870026525199, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7324569', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2285, 'n_words': 538}