src/Entity/StravaProfile.php line 37

Open in your IDE?
  1. <?php
  2. // src/Entity/StravaProfile.php
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Repository\StravaProfileRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. /**
  12.  * @ORM\Entity(repositoryClass=StravaProfileRepository::class)
  13.  * @ORM\Table(name="strava_profile", uniqueConstraints={
  14.  *     @ORM\UniqueConstraint(name="unique_strava_athlete", columns={"strava_athlete_id"})
  15.  * })
  16.  * @ApiResource(
  17.  *     normalizationContext={"groups"={"strava_profile:read"}},
  18.  *     denormalizationContext={"groups"={"strava_profile:write"}},
  19.  *     itemOperations={
  20.  *         "get"={
  21.  *             "security"="is_granted('ROLE_ADMIN') or object.getUser() == user"
  22.  *         },
  23.  *         "delete"={
  24.  *             "security"="is_granted('ROLE_ADMIN') or object.getUser() == user"
  25.  *         }
  26.  *     },
  27.  *     collectionOperations={
  28.  *         "get"={
  29.  *             "security"="is_granted('ROLE_ADMIN')"
  30.  *         }
  31.  *     }
  32.  * )
  33.  */
  34. class StravaProfile
  35. {
  36.     /**
  37.      * @ORM\Id
  38.      * @ORM\GeneratedValue
  39.      * @ORM\Column(type="integer")
  40.      * @Groups({"strava_profile:read", "user:read"})
  41.      */
  42.     private $id;
  43.     /**
  44.      * @ORM\OneToOne(targetEntity=User::class, inversedBy="stravaProfile")
  45.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  46.      * @Groups({"strava_profile:read"})
  47.      */
  48.     private $user;
  49.     /**
  50.      * @ORM\Column(type="bigint", unique=true)
  51.      * @Groups({"strava_profile:read"})
  52.      */
  53.     private $stravaAthleteId;
  54.     /**
  55.      * @ORM\Column(type="text")
  56.      */
  57.     private $accessToken;
  58.     /**
  59.      * @ORM\Column(type="text")
  60.      */
  61.     private $refreshToken;
  62.     /**
  63.      * @ORM\Column(type="integer")
  64.      */
  65.     private $expiresAt;
  66.     /**
  67.      * @ORM\Column(type="string", length=255, nullable=true)
  68.      * @Groups({"strava_profile:read"})
  69.      */
  70.     private $scope;
  71.     /**
  72.      * @ORM\OneToMany(targetEntity=StravaActivity::class, mappedBy="stravaProfile", orphanRemoval=true, cascade={"persist", "remove"})
  73.      * @Groups({"strava_profile:read"})
  74.      */
  75.     private $activities;
  76.     /**
  77.      * @ORM\Column(type="datetime")
  78.      * @Gedmo\Timestampable(on="create")
  79.      * @Groups({"strava_profile:read"})
  80.      */
  81.     private $createdAt;
  82.     /**
  83.      * @ORM\Column(type="datetime", nullable=true)
  84.      * @Gedmo\Timestampable(on="update")
  85.      */
  86.     private $updatedAt;
  87.     public function __construct()
  88.     {
  89.         $this->activities = new ArrayCollection();
  90.     }
  91.     public function getId(): ?int
  92.     {
  93.         return $this->id;
  94.     }
  95.     public function getUser(): ?User
  96.     {
  97.         return $this->user;
  98.     }
  99.     public function setUser(User $user): self
  100.     {
  101.         $this->user = $user;
  102.         return $this;
  103.     }
  104.     public function getStravaAthleteId(): ?int
  105.     {
  106.         return $this->stravaAthleteId;
  107.     }
  108.     public function setStravaAthleteId(int $stravaAthleteId): self
  109.     {
  110.         $this->stravaAthleteId = $stravaAthleteId;
  111.         return $this;
  112.     }
  113.     public function getAccessToken(): ?string
  114.     {
  115.         return $this->accessToken;
  116.     }
  117.     public function setAccessToken(string $accessToken): self
  118.     {
  119.         $this->accessToken = $accessToken;
  120.         return $this;
  121.     }
  122.     public function getRefreshToken(): ?string
  123.     {
  124.         return $this->refreshToken;
  125.     }
  126.     public function setRefreshToken(string $refreshToken): self
  127.     {
  128.         $this->refreshToken = $refreshToken;
  129.         return $this;
  130.     }
  131.     public function getExpiresAt(): ?int
  132.     {
  133.         return $this->expiresAt;
  134.     }
  135.     public function setExpiresAt(int $expiresAt): self
  136.     {
  137.         $this->expiresAt = $expiresAt;
  138.         return $this;
  139.     }
  140.     public function isTokenExpired(): bool
  141.     {
  142.         return time() >= $this->expiresAt;
  143.     }
  144.     public function getScope(): ?string
  145.     {
  146.         return $this->scope;
  147.     }
  148.     public function setScope(?string $scope): self
  149.     {
  150.         $this->scope = $scope;
  151.         return $this;
  152.     }
  153.     /**
  154.      * @return Collection<int, StravaActivity>
  155.      */
  156.     public function getActivities(): Collection
  157.     {
  158.         return $this->activities;
  159.     }
  160.     public function addActivity(StravaActivity $activity): self
  161.     {
  162.         if (!$this->activities->contains($activity)) {
  163.             $this->activities[] = $activity;
  164.             $activity->setStravaProfile($this);
  165.         }
  166.         return $this;
  167.     }
  168.     public function removeActivity(StravaActivity $activity): self
  169.     {
  170.         if ($this->activities->removeElement($activity)) {
  171.             if ($activity->getStravaProfile() === $this) {
  172.                 $activity->setStravaProfile(null);
  173.             }
  174.         }
  175.         return $this;
  176.     }
  177.     public function getCreatedAt(): ?\DateTimeInterface
  178.     {
  179.         return $this->createdAt;
  180.     }
  181.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  182.     {
  183.         $this->createdAt = $createdAt;
  184.         return $this;
  185.     }
  186.     public function getUpdatedAt(): ?\DateTimeInterface
  187.     {
  188.         return $this->updatedAt;
  189.     }
  190.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  191.     {
  192.         $this->updatedAt = $updatedAt;
  193.         return $this;
  194.     }
  195. }