Created
July 6, 2015 17:48
-
-
Save cawa87/f38dabf4aca7e4290271 to your computer and use it in GitHub Desktop.
secretcloset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use AppBundle\Common\Persistence\DependencyInjection\ContainerAware; | |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
class AccessTokenManager implements ContainerAwareInterface | |
{ | |
use ContainerAware; | |
public function createAccessToken(User $user, $scopeRequest = true) | |
{ | |
$em = $this->container->get('doctrine.orm.default_entity_manager'); | |
$this->clean($user); | |
$accessToken = new AccessToken($this->container->get('fos_user.util.token_generator')->generateToken()); | |
$accessToken->setUser($user); | |
if ($scopeRequest) { | |
$request = $this->container->get('request'); | |
$accessToken->setUserAgent($request->headers->get('User-Agent')); | |
$accessToken->setClientIp($request->getClientIp()); | |
} | |
$em->persist($accessToken); | |
$em->flush(); | |
return $accessToken; | |
} | |
public function clean(User $user) | |
{ | |
$em = $this->get('doctrine.orm.default_entity_manager'); | |
$repo = $em->getRepository('AppBundle:AccessToken'); | |
$repo->deleteAllByUser($user); | |
} | |
public function getOrCreateAccessToken(User $user, $scopeRequest = true) | |
{ | |
$accessToken = $user->getAccessToken(); | |
if (!$accessToken) { | |
$accessToken = $this->createAccessToken($user, $scopeRequest); | |
} | |
return $accessToken; | |
} | |
public function getOrCreateAccessTokenByEmail($email, $scopeRequest = true) | |
{ | |
/** @var \FOS\UserBundle\Doctrine\UserManager $userManager */ | |
$userManager = $this->container->get('fos_user.user_manager'); | |
$user = $userManager->findUserByEmail($email); | |
if (!$user) { | |
throw new NotFoundHttpException; | |
} | |
return $this->getOrCreateAccessToken($user, $scopeRequest); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use AppBundle\Entity\ItemBrandRepository; | |
use FOS\RestBundle\Controller\Annotations as Rest; | |
use FOS\RestBundle\Controller\FOSRestController; | |
use FOS\RestBundle\Request\ParamFetcherInterface; | |
use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
class ItemBrandsController extends FOSRestController | |
{ | |
/** | |
* @ApiDoc( | |
* section="Бренды", | |
* tags={"beta"}, | |
* description="Получить полный список брендов", | |
* statusCodes={ | |
* 200: "Информация предоставлена", | |
* 204: "Брендов нет" | |
* } | |
* ) | |
* | |
* @Rest\QueryParam(name="items") | |
* @Rest\QueryParam(name="city") | |
* @Rest\Get("/item-brands") | |
*/ | |
public function getItemBrandsAction(ParamFetcherInterface $paramFetcher) | |
{ | |
/** @var ItemBrandRepository $repo */ | |
$repo = $this->getDoctrine()->getRepository('AppBundle:ItemBrand'); | |
$items = $paramFetcher->get('items') | |
? $repo->fetchListWithItems($paramFetcher->get('city')) | |
: $repo->fetchList(); | |
if ($items) { | |
return $items; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\EntityRepository; | |
class CityRepository extends EntityRepository | |
{ | |
public function fetchList() | |
{ | |
$qb = $this->createQueryBuilder('city'); | |
$q = $qb->select([ | |
'city.id', | |
'city.title', | |
'city.titleShort short_title', | |
'city.slug', | |
'city.maxPrice max_price', | |
'currency.code currency_code', | |
]); | |
$q->join('city.country', 'country'); | |
$q->join('country.currency', 'currency'); | |
$cities = $q->getQuery()->getArrayResult(); | |
$ids = []; | |
foreach ($cities as $city) { | |
if (!$city['max_price']) { | |
$ids[] = $city['id']; | |
} | |
} | |
$c = $this->_em->getConnection(); | |
$s = $c->prepare(' | |
SELECT | |
i.city_id, | |
MAX(i.price) max_price | |
FROM | |
items i | |
WHERE | |
i.city_id IN (' . implode(',', $ids) . ') | |
AND item_status(i.id) IN ("active", "moderation") | |
GROUP BY | |
i.city_id | |
'); | |
$s->execute(); | |
foreach ($s->fetchAll() as $city_) { | |
$cityId = (int)$city_['city_id']; | |
$maxPrice = (int)$city_['max_price']; | |
foreach ($cities as & $city) { | |
if ($city['id'] == $cityId) { | |
$city['max_price'] = $maxPrice; | |
break; | |
} | |
} | |
} | |
return $cities; | |
} | |
public function fetchListWithItems() | |
{ | |
$qb1 = $this->_em->createQueryBuilder(); | |
$q1 = $qb1->select('q1_city'); | |
$q1->from('AppBundle:Item', 'q1_item'); | |
$q1->join('q1_item.city', 'q1_city'); | |
$q1->where($qb1->expr()->in('itemStatus(q1_item)', ['active', 'moderation'])); | |
$q1->groupBy('q1_city'); | |
$qb = $this->createQueryBuilder('city'); | |
$q = $qb->select(['city.slug', 'currency.code currency_code']); | |
$q->join('city.country', 'country'); | |
$q->join('country.currency', 'currency'); | |
$q->where($qb->expr()->in('city', $q1->getDQL())); | |
return $q->getQuery()->getArrayResult(); | |
} | |
public function fetchOneByCoords($lat, $lon) | |
{ | |
$qb = $this->createQueryBuilder('city'); | |
$q = $qb->select([ | |
'city.slug', | |
'currency.code currency_code', | |
'(gLength(lineStringFromWKB(lineString(city.loc, point(:lon, :lat))))) hidden distance', | |
]); | |
$q->join('city.country', 'country'); | |
$q->join('country.currency', 'currency'); | |
$q->orderBy('distance', 'asc'); | |
$q->setMaxResults(1); | |
$q->setParameter('lon', $lon); | |
$q->setParameter('lat', $lat); | |
return $q->getQuery()->getSingleResult(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Авторизация в апи по access_token
Часть кода уже покрыта тестами
Много логики в мускуль-процедурах
Sf2.6.* + FosRestBundle и конечно же доктрина