<?php
// src/Entity/StravaProfile.php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\StravaProfileRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=StravaProfileRepository::class)
* @ORM\Table(name="strava_profile", uniqueConstraints={
* @ORM\UniqueConstraint(name="unique_strava_athlete", columns={"strava_athlete_id"})
* })
* @ApiResource(
* normalizationContext={"groups"={"strava_profile:read"}},
* denormalizationContext={"groups"={"strava_profile:write"}},
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_ADMIN') or object.getUser() == user"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN') or object.getUser() == user"
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* }
* )
*/
class StravaProfile
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"strava_profile:read", "user:read"})
*/
private $id;
/**
* @ORM\OneToOne(targetEntity=User::class, inversedBy="stravaProfile")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"strava_profile:read"})
*/
private $user;
/**
* @ORM\Column(type="bigint", unique=true)
* @Groups({"strava_profile:read"})
*/
private $stravaAthleteId;
/**
* @ORM\Column(type="text")
*/
private $accessToken;
/**
* @ORM\Column(type="text")
*/
private $refreshToken;
/**
* @ORM\Column(type="integer")
*/
private $expiresAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"strava_profile:read"})
*/
private $scope;
/**
* @ORM\OneToMany(targetEntity=StravaActivity::class, mappedBy="stravaProfile", orphanRemoval=true, cascade={"persist", "remove"})
* @Groups({"strava_profile:read"})
*/
private $activities;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
* @Groups({"strava_profile:read"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
public function __construct()
{
$this->activities = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
public function getStravaAthleteId(): ?int
{
return $this->stravaAthleteId;
}
public function setStravaAthleteId(int $stravaAthleteId): self
{
$this->stravaAthleteId = $stravaAthleteId;
return $this;
}
public function getAccessToken(): ?string
{
return $this->accessToken;
}
public function setAccessToken(string $accessToken): self
{
$this->accessToken = $accessToken;
return $this;
}
public function getRefreshToken(): ?string
{
return $this->refreshToken;
}
public function setRefreshToken(string $refreshToken): self
{
$this->refreshToken = $refreshToken;
return $this;
}
public function getExpiresAt(): ?int
{
return $this->expiresAt;
}
public function setExpiresAt(int $expiresAt): self
{
$this->expiresAt = $expiresAt;
return $this;
}
public function isTokenExpired(): bool
{
return time() >= $this->expiresAt;
}
public function getScope(): ?string
{
return $this->scope;
}
public function setScope(?string $scope): self
{
$this->scope = $scope;
return $this;
}
/**
* @return Collection<int, StravaActivity>
*/
public function getActivities(): Collection
{
return $this->activities;
}
public function addActivity(StravaActivity $activity): self
{
if (!$this->activities->contains($activity)) {
$this->activities[] = $activity;
$activity->setStravaProfile($this);
}
return $this;
}
public function removeActivity(StravaActivity $activity): self
{
if ($this->activities->removeElement($activity)) {
if ($activity->getStravaProfile() === $this) {
$activity->setStravaProfile(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}