auctions-1.0.x-dev/modules/auctions_core/src/Controller/RefreshBidController.php
modules/auctions_core/src/Controller/RefreshBidController.php
<?php
namespace Drupal\auctions_core\Controller;
use Drupal\auctions_core\AuctionToolsTrait;
use Drupal\auctions_core\Entity\AuctionItem;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
/**
*
*/
class RefreshBidController extends ControllerBase {
use AuctionToolsTrait;
/**
* Current user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
protected $config_factory;
/**
* The current request
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs a new SettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
*/
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $currentUser, RequestStack $request_stack) {
$this->config_factory = $config_factory;
$this->currentUser = $currentUser;
$this->request = $request_stack->getCurrentRequest();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('current_user'),
$container->get('request_stack'),
);
}
/**
* Use the AuctionTools service to get the highest bid.
*/
public function getHighestBidJson(AuctionItem $auction_item) {
$highestBidValue = $auction_item->seekCurrentHightest();
$response = [];
$response['op'] = 'noop';
$response['event'] = \floor(\microtime(TRUE) * 1000);
$response['ip'] = $this->request->getClientIp();
$response['hasPrice'] = FALSE;
$response['isOpen'] = $auction_item->isOpen();
$uid = $this->currentUser()->id();
$response['uid'] = $uid;
if (isset($highestBidValue['minPrice'])) {
$selfBid = ($highestBidValue['leadBid'] && $highestBidValue['leadBid']->getOwnerId() == $uid) ? TRUE : FALSE;
$response['selfBid'] = $selfBid ? $this->selfBidPhrase() : FALSE;
$config = $this->config('auctions.item_settings');
$thous = $config->get('thousand-separator');
$dec = $config->get('decimal-separator');
$response['op'] = 'refresh';
$response['minPrice'] = $highestBidValue['minPrice'];
$response['hasPrice'] = TRUE;
$response['bidStep'] = $auction_item->getBidStep();
$response['refreshAmount'] = $auction_item->roundCents($response['minPrice'] + $response['bidStep']);
$response['refreshMin'] = $auction_item->roundCents($response['minPrice'] + .01);
$response['formattedPrice'] = $auction_item->showAsCents($response['minPrice'], $dec, $thous);
}
// Use State API to allow a logging activity. Load reminder!
$bidRefreshLog = \Drupal::state()->get('auctions.bid_refresh_log');
if ($bidRefreshLog){
\Drupal::logger('auctions_core.bid_refresh')->notice('Refresh Bid Conroller hit [@response]',['@response'=> \json_encode($response)]);
}
return new JsonResponse($response);
}
}
